diff --git a/README.md b/README.md index 5e56c12..49527ea 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,11 @@ Several project euler problems solved in C++ # Euler problem 1 :

If we list all the natural numbers below $10$ that are multiples of $3$ or $5$, we get $3, 5, 6$ and $9$. The sum of these multiples is $23$.

-

Find the sum of all the multiples of $3$ or $5$ below $1000$.

\ No newline at end of file +

Find the sum of all the multiples of $3$ or $5$ below $1000$.

+ + +# Euler problem 2 : + +

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, \dots$$

+

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

diff --git a/euler/euler/euler.cpp b/euler/euler/euler.cpp index 62db2f0..83da8ab 100644 --- a/euler/euler/euler.cpp +++ b/euler/euler/euler.cpp @@ -4,12 +4,14 @@ #include #include #include "euler_p1.h" +#include "euler_p2.h" void unit_tests() { assert(euler1(10) == 23); + assert(euler2(10) == 10); std::cout << "All tests passed! continues main." << std::endl; } @@ -19,10 +21,16 @@ int euler_p1() { return res; } +int euler_p2() { + int res = euler2(4000000); + return res; +} + int main() { unit_tests(); std::cout << "the result for problem 1 = " << euler_p1() << std::endl; + std::cout << "the result for problem 2 = " << euler_p2() << std::endl; } diff --git a/euler/euler/euler.vcxproj b/euler/euler/euler.vcxproj index 6ab2224..e3aaede 100644 --- a/euler/euler/euler.vcxproj +++ b/euler/euler/euler.vcxproj @@ -129,9 +129,11 @@ + + diff --git a/euler/euler/euler.vcxproj.filters b/euler/euler/euler.vcxproj.filters index 104f92e..bc124d2 100644 --- a/euler/euler/euler.vcxproj.filters +++ b/euler/euler/euler.vcxproj.filters @@ -21,10 +21,16 @@ Source Files + + Source Files + Source Files + + Source Files + \ No newline at end of file diff --git a/euler/euler/euler_p2.cpp b/euler/euler/euler_p2.cpp new file mode 100644 index 0000000..5a9b907 --- /dev/null +++ b/euler/euler/euler_p2.cpp @@ -0,0 +1,26 @@ +#include "euler_p2.h" + +/* +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. +*/ + +int euler2(int limit) { + + int current = 1; + int previous = 1; + int temp = 0; + int result = 0; + + while (temp < limit) { + temp = current + previous; + previous = current; + current = temp; + if (temp % 2 == 0) + { + result += temp; + } + } + return result; +} \ No newline at end of file diff --git a/euler/euler/euler_p2.h b/euler/euler/euler_p2.h new file mode 100644 index 0000000..b1c4915 --- /dev/null +++ b/euler/euler/euler_p2.h @@ -0,0 +1,13 @@ +#pragma once +#pragma once +/** + * @brief Solves Euler problem 2: Even Fibonacci Numbers + * + * This function calculates the sum of all the even numbers in the fibonacchi sequence with the upper limit as close to given limit + * + * @param limit The upper limit for the range to consider (should be positive). + * @return The sum of every even fibbonachi number lower then the limit. + * + * @note This function only works with positive integer inputs. + */ +int euler2(int limit); \ No newline at end of file