Skip to content

Commit

Permalink
add solution 2 in C++, tweak docs
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 21, 2024
1 parent 13df69b commit efb3d71
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
Expand Down
1 change: 1 addition & 0 deletions cplusplus/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,5 @@ Problems Solved
---------------

- ☒ `1 <./p0001.cpp>`__
- ☒ `2 <./p0002.cpp>`__

45 changes: 45 additions & 0 deletions cplusplus/p0002.cpp
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

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
5 changes: 3 additions & 2 deletions cplusplus/test_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

answers = {
1: 233168,
2: 4613732,
}

# this is the set of problems where I have the right answer but wrong solution
Expand Down Expand Up @@ -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
Expand All @@ -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']):
Expand Down
8 changes: 8 additions & 0 deletions docs/cplusplus/p0002.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
C++ Implementation of Problem 2
===============================

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/cplusplus/p0002.cpp>`_

.. literalinclude:: ../../cplusplus/p0002.cpp
:language: C++
:linenos:
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
+-----------+------+------+------+------+------+------+
Expand Down

0 comments on commit efb3d71

Please sign in to comment.