diff --git a/README.rst b/README.rst index 5a715dfa..64ab720c 100644 --- a/README.rst +++ b/README.rst @@ -43,8 +43,8 @@ LivInTheLookingGlass’s Project Euler solutions | | ``msvc``, |br| | | | | | ``pcc``, ``tcc`` | | | +------------+---------------------+--------+-------------------+ -| C++ | C++98+ in |br| | 1 | |Cpi| | -| | ``g++``, ``clang``, | | | +| C++ | C++98+ in |br| | 2 | |Cpi| | +| | ``gcc``, ``clang``, | | | | | ``msvc`` | | | +------------+---------------------+--------+-------------------+ | C# | .NET 2+ | 6 | |C#i| | diff --git a/cplusplus/README.rst b/cplusplus/README.rst index b844f0f5..d4e841e2 100644 --- a/cplusplus/README.rst +++ b/cplusplus/README.rst @@ -155,4 +155,5 @@ Problems Solved --------------- - ☒ `1 <./p0001.cpp>`__ +- ☒ `2 <./p0002.cpp>`__ diff --git a/cplusplus/p0002.cpp b/cplusplus/p0002.cpp new file mode 100644 index 00000000..87f2389b --- /dev/null +++ b/cplusplus/p0002.cpp @@ -0,0 +1,45 @@ +/* +Project Euler Problem 2 + +Again, see Python implementation for a proof + +Problem: + +Each new term in the Fibonacci sequence is generated by adding the previous two +terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... + +By considering the terms in the Fibonacci sequence whose values do not exceed +four million, find the sum of the even-valued terms. +*/ + +#ifndef EULER_P0002 +#define EULER_P0002 +#include + +unsigned long long p0002() { + unsigned long long answer = 0, + a = 1, b = 2, t; + while (b < 4000000) { + // odd (1, 3, 13, 55, ...) + // odd (1, 5, 21, 89, ...) + // even (2, 8, 34, 144, ...) + answer += b; + for (unsigned char z = 0; z < 3; z++) { + t = b; + b = a + b; + a = t; + } + } + return answer; +} + +#ifndef UNITY_END +int main(int argc, char const *argv[]) { + unsigned long long answer = p0002(); + printf("%llu\n", answer); + return 0; +} +#endif +#endif diff --git a/cplusplus/test_euler.py b/cplusplus/test_euler.py index 0f744492..9e84b59e 100644 --- a/cplusplus/test_euler.py +++ b/cplusplus/test_euler.py @@ -21,6 +21,7 @@ answers = { 1: 233168, + 2: 4613732, } # this is the set of problems where I have the right answer but wrong solution @@ -78,7 +79,7 @@ warn("Could not detect system architecture, defaulting to .exe") EXE_EXT = "exe" -GCC_BINARY = environ.get('GCC_OVERRIDE', 'g++') +GCC_BINARY = environ.get('GCC_OVERRIDE', 'gcc') AOCC_BINARY = environ.get('AOCC_OVERRIDE', 'clang') # compiler variables section @@ -88,7 +89,7 @@ for comp in environ['COMPILER_OVERRIDE'].upper().split(','): compilers.extend(f'{comp}+{std}' for std in STANDARDS) else: - if not (IN_TERMUX and GCC_BINARY == 'g++') and which(GCC_BINARY): # Termux maps gcc->clang + if not (IN_TERMUX and GCC_BINARY == 'gcc') and which(GCC_BINARY): # Termux maps gcc->clang compilers.extend(f'GCC+{std}' for std in STANDARDS) if which('clang'): if b'AOCC' in check_output(['clang', '--version']): diff --git a/docs/cplusplus/p0002.rst b/docs/cplusplus/p0002.rst new file mode 100644 index 00000000..9f0c7d63 --- /dev/null +++ b/docs/cplusplus/p0002.rst @@ -0,0 +1,8 @@ +C++ Implementation of Problem 2 +=============================== + +View source code `here on GitHub! `_ + +.. literalinclude:: ../../cplusplus/p0002.cpp + :language: C++ + :linenos: diff --git a/docs/index.rst b/docs/index.rst index ee1f96e5..9052327d 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -102,7 +102,7 @@ This project is divided into several Makefiles, connected by a root Makefile whi +-----------+------+------+------+------+------+------+ |:prob:`1` | |d| | |d| | |d| | |d| | |d| | |d| | +-----------+------+------+------+------+------+------+ -|:prob:`2` | |d| | | |d| | |d| | |d| | |d| | +|:prob:`2` | |d| | |d| | |d| | |d| | |d| | |d| | +-----------+------+------+------+------+------+------+ |:prob:`3` | |d| | | | | |d| | |d| | +-----------+------+------+------+------+------+------+