Skip to content

Commit

Permalink
solve p0004 in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 22, 2024
1 parent bcc14e4 commit 21cb461
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ LivInTheLookingGlass’s Project Euler solutions
| | ``msvc``, |br| | | |
| | ``pcc``, ``tcc`` | | |
+------------+---------------------+--------+-------------------+
| C++ | C++98+ in |br| | 2 | |Cpi| |
| C++ | C++98+ in |br| | 3 | |Cpi| |
| | ``gcc``, ``clang``, | | |
| | ``msvc`` | | |
+------------+---------------------+--------+-------------------+
Expand Down
5 changes: 2 additions & 3 deletions cplusplus/p0001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
#ifndef EULER_P0001
#define EULER_P0001
#include <stdio.h>
#include <iostream>

unsigned long long p0001() {
unsigned long long answer = 0;
Expand All @@ -32,8 +32,7 @@ unsigned long long p0001() {

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0001();
printf("%llu\n", answer);
std::cout << p0001() << std::endl;
return 0;
}
#endif
Expand Down
5 changes: 2 additions & 3 deletions cplusplus/p0002.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ four million, find the sum of the even-valued terms.

#ifndef EULER_P0002
#define EULER_P0002
#include <stdio.h>
#include <iostream>

unsigned long long p0002() {
unsigned long long answer = 0,
Expand All @@ -37,8 +37,7 @@ unsigned long long p0002() {

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0002();
printf("%llu\n", answer);
std::cout << p0002() << std::endl;
return 0;
}
#endif
Expand Down
40 changes: 40 additions & 0 deletions cplusplus/p0004.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Project Euler Problem 4
This was pretty easy to do, given the C++ stdlib
Problem:
A palindromic number reads the same both ways. The largest palindrome made from
the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
*/
#ifndef EULER_P0004
#define EULER_P0004
#include <algorithm>
#include <iostream>

unsigned int p0004() {
unsigned int answer = 0, i, j, prod;
for (i = 100; i < 1000; i++) {
for (j = 100; j < 1000; j++) {
prod = i * j;
std::string forward = std::to_string(prod);
std::string reverse = forward;
std::reverse(reverse.begin(), reverse.end());
if (forward == reverse) {
answer = std::max(answer, prod);
}
}
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
std::cout << p0004() << std::endl;
return 0;
}
#endif
#endif
5 changes: 3 additions & 2 deletions cplusplus/test_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
answers = {
1: 233168,
2: 4613732,
4: 906609,
}

# this is the set of problems where I have the right answer but wrong solution
Expand Down Expand Up @@ -133,10 +134,10 @@
EXE_TEMPLATE = "{}{}p{{:0>4}}.{{}}.{}".format(BUILD_FOLDER, sep, EXE_EXT)
# include sep in the recipe so that Windows won't complain

GCC_TEMPLATE = "{} {{}} -O2 -lm -Wall -Werror -std={} -march=native -flto -fwhole-program -o {{}}"
GCC_TEMPLATE = "{} {{}} -O2 -lstdc++ -lm -Wall -Werror -std={} -march=native -flto -fwhole-program -o {{}}"
if environ.get('COV') == 'true':
GCC_TEMPLATE += ' -ftest-coverage -fprofile-arcs'
CLANG_TEMPLATE = "{} {{}} -O2 {} {} -Wall -Werror -std={} {} -o {{}}"
CLANG_TEMPLATE = "{} {{}} -O2 -lstdc++ {} {} -Wall -Werror -std={} {} -o {{}}"

templates = {}
for std in STANDARDS:
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ This project is divided into several Makefiles, connected by a root Makefile whi
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`3` |:c-d:`0003` | | | |:py-d:`0003`|:rs-d:`0003`|
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`4` |:c-d:`0004` | |:cs-d:`0004`|:js-d:`0004`|:py-d:`0004`|:rs-d:`0004`|
|:prob:`4` |:c-d:`0004` |:cp-d:`0004`|:cs-d:`0004`|:js-d:`0004`|:py-d:`0004`|:rs-d:`0004`|
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`5` |:c-d:`0005` | | | |:py-d:`0005`|:rs-d:`0005`|
+-----------+------------+------------+------------+------------+------------+------------+
Expand Down

0 comments on commit 21cb461

Please sign in to comment.