Compare commits
2 Commits
dddd26538d
...
d628bc9c78
Author | SHA1 | Date | |
---|---|---|---|
d628bc9c78 | |||
99bfe1c681 |
@ -6,3 +6,10 @@ Several project euler problems solved in C++
|
|||||||
|
|
||||||
<p>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$.</p>
|
<p>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$.</p>
|
||||||
<p>Find the sum of all the multiples of $3$ or $5$ below $1000$.</p>
|
<p>Find the sum of all the multiples of $3$ or $5$ below $1000$.</p>
|
||||||
|
|
||||||
|
|
||||||
|
# Euler problem 2 :
|
||||||
|
|
||||||
|
<p>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$$</p>
|
||||||
|
<p>By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.</p>
|
||||||
|
@ -4,12 +4,14 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include "euler_p1.h"
|
#include "euler_p1.h"
|
||||||
|
#include "euler_p2.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void unit_tests() {
|
void unit_tests() {
|
||||||
|
|
||||||
assert(euler1(10) == 23);
|
assert(euler1(10) == 23);
|
||||||
|
assert(euler2(10) == 10);
|
||||||
|
|
||||||
std::cout << "All tests passed! continues main." << std::endl;
|
std::cout << "All tests passed! continues main." << std::endl;
|
||||||
}
|
}
|
||||||
@ -19,10 +21,16 @@ int euler_p1() {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int euler_p2() {
|
||||||
|
int res = euler2(4000000);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
unit_tests();
|
unit_tests();
|
||||||
|
|
||||||
std::cout << "the result for problem 1 = " << euler_p1() << std::endl;
|
std::cout << "the result for problem 1 = " << euler_p1() << std::endl;
|
||||||
|
std::cout << "the result for problem 2 = " << euler_p2() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,9 +129,11 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="euler.cpp" />
|
<ClCompile Include="euler.cpp" />
|
||||||
<ClCompile Include="euler_p1.cpp" />
|
<ClCompile Include="euler_p1.cpp" />
|
||||||
|
<ClCompile Include="euler_p2.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="euler_p1.h" />
|
<ClInclude Include="euler_p1.h" />
|
||||||
|
<ClInclude Include="euler_p2.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
@ -21,10 +21,16 @@
|
|||||||
<ClCompile Include="euler_p1.cpp">
|
<ClCompile Include="euler_p1.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="euler_p2.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="euler_p1.h">
|
<ClInclude Include="euler_p1.h">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="euler_p2.h">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
26
euler/euler/euler_p2.cpp
Normal file
26
euler/euler/euler_p2.cpp
Normal file
@ -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;
|
||||||
|
}
|
13
euler/euler/euler_p2.h
Normal file
13
euler/euler/euler_p2.h
Normal file
@ -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);
|
Loading…
x
Reference in New Issue
Block a user