From 21cb4616f05c480210f80b710f5bd7335675c229 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Mon, 22 Jul 2024 11:23:58 -0500 Subject: [PATCH] solve p0004 in C++ --- README.rst | 2 +- cplusplus/p0001.cpp | 5 ++--- cplusplus/p0002.cpp | 5 ++--- cplusplus/p0004.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ cplusplus/test_euler.py | 5 +++-- docs/index.rst | 2 +- 6 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 cplusplus/p0004.cpp diff --git a/README.rst b/README.rst index 977231ce..b3c93598 100644 --- a/README.rst +++ b/README.rst @@ -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`` | | | +------------+---------------------+--------+-------------------+ diff --git a/cplusplus/p0001.cpp b/cplusplus/p0001.cpp index 26724a1f..9fbdeea9 100644 --- a/cplusplus/p0001.cpp +++ b/cplusplus/p0001.cpp @@ -14,7 +14,7 @@ */ #ifndef EULER_P0001 #define EULER_P0001 -#include +#include unsigned long long p0001() { unsigned long long answer = 0; @@ -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 diff --git a/cplusplus/p0002.cpp b/cplusplus/p0002.cpp index 87f2389b..765c0b17 100644 --- a/cplusplus/p0002.cpp +++ b/cplusplus/p0002.cpp @@ -16,7 +16,7 @@ four million, find the sum of the even-valued terms. #ifndef EULER_P0002 #define EULER_P0002 -#include +#include unsigned long long p0002() { unsigned long long answer = 0, @@ -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 diff --git a/cplusplus/p0004.cpp b/cplusplus/p0004.cpp new file mode 100644 index 00000000..29e71598 --- /dev/null +++ b/cplusplus/p0004.cpp @@ -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 +#include + +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 diff --git a/cplusplus/test_euler.py b/cplusplus/test_euler.py index 9e84b59e..143f3fda 100644 --- a/cplusplus/test_euler.py +++ b/cplusplus/test_euler.py @@ -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 @@ -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: diff --git a/docs/index.rst b/docs/index.rst index 02acb6b7..c36f457c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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`| +-----------+------------+------------+------------+------------+------------+------------+