Compare commits
10 Commits
euler_prob
...
main
Author | SHA1 | Date | |
---|---|---|---|
7c11371d49 | |||
8e26719010 | |||
c05f3dd6e2 | |||
a5c9415d2a | |||
54804f195c | |||
d0f9f0ede3 | |||
d628bc9c78 | |||
99bfe1c681 | |||
dddd26538d | |||
eb67e67750 |
18
README.md
18
README.md
@ -4,5 +4,19 @@ Several project euler problems solved in C++
|
||||
|
||||
# Euler problem 1 :
|
||||
|
||||
<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>
|
||||
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.
|
||||
|
||||
|
||||
# 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,
|
||||
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
|
||||
|
||||
# Euler problem 3 :
|
||||
|
||||
The prime factors of 13195 are 5, 7, 13 and 29
|
||||
What is the largest prime factor of the number 600851475143
|
||||
|
||||
[wiki](https://baobabfruit.ddns.net/en/code/project-euler/prime-numbers)
|
@ -4,12 +4,18 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include "euler_p1.h"
|
||||
#include "euler_p2.h"
|
||||
#include "euler_p3.h"
|
||||
#include "euler_p4.h"
|
||||
|
||||
|
||||
|
||||
void unit_tests() {
|
||||
|
||||
assert(euler1(10) == 23);
|
||||
assert(euler2(10) == 10);
|
||||
assert(euler3(13195) == 29);
|
||||
assert(euler4(99,99) == 9009);
|
||||
|
||||
std::cout << "All tests passed! continues main." << std::endl;
|
||||
}
|
||||
@ -19,10 +25,26 @@ int euler_p1() {
|
||||
return res;
|
||||
}
|
||||
|
||||
int euler_p2() {
|
||||
int res = euler2(4000000);
|
||||
return res;
|
||||
}
|
||||
|
||||
long long int euler_p3() {
|
||||
long long int res = euler3(600851475143);
|
||||
return res;
|
||||
}
|
||||
|
||||
int euler_p4() {
|
||||
int res = euler4(999, 999);
|
||||
}
|
||||
|
||||
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;
|
||||
std::cout << "the result for problem 3 = " << euler_p3() << std::endl;
|
||||
}
|
||||
|
||||
|
@ -129,9 +129,15 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="euler.cpp" />
|
||||
<ClCompile Include="euler_p1.cpp" />
|
||||
<ClCompile Include="euler_p2.cpp" />
|
||||
<ClCompile Include="euler_p3.cpp" />
|
||||
<ClCompile Include="euler_p4.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="euler_p1.h" />
|
||||
<ClInclude Include="euler_p2.h" />
|
||||
<ClInclude Include="euler_p3.h" />
|
||||
<ClInclude Include="euler_p4.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
@ -21,10 +21,28 @@
|
||||
<ClCompile Include="euler_p1.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="euler_p2.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="euler_p3.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="euler_p4.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="euler_p1.h">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="euler_p2.h">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="euler_p3.h">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="euler_p4.h">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</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;
|
||||
}
|
12
euler/euler/euler_p2.h
Normal file
12
euler/euler/euler_p2.h
Normal file
@ -0,0 +1,12 @@
|
||||
#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);
|
30
euler/euler/euler_p3.cpp
Normal file
30
euler/euler/euler_p3.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include "euler_p3.h"
|
||||
#include <vector>
|
||||
|
||||
|
||||
/*
|
||||
The prime factors of 13195 are 5, 7, 13 and 29
|
||||
What is the largest prime factor of the number 600851475143
|
||||
*/
|
||||
|
||||
long long int euler3(long long int limit) {
|
||||
long long int largest_factor = 0;
|
||||
|
||||
|
||||
while (limit % 2 == 0) {
|
||||
largest_factor = 2;
|
||||
limit /= 2;
|
||||
}
|
||||
for (long long int i = 3; i <= sqrt(limit); i += 2) {
|
||||
while (limit % i == 0) {
|
||||
largest_factor = i;
|
||||
limit /= i;
|
||||
}
|
||||
}
|
||||
|
||||
if (limit > 2) {
|
||||
largest_factor = limit;
|
||||
}
|
||||
|
||||
return largest_factor;
|
||||
}
|
12
euler/euler/euler_p3.h
Normal file
12
euler/euler/euler_p3.h
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
/**
|
||||
* @brief Solves Euler problem 3: Largest prime factor
|
||||
*
|
||||
* This function calculates the highest prime number that devides the given limit
|
||||
*
|
||||
* @param limit The upper limit for the range to consider (should be positive).
|
||||
* @return The highest prime lower or equal to the limit.
|
||||
*
|
||||
* @note This function only works with positive integer inputs.
|
||||
*/
|
||||
long long int euler3(long long int limit);
|
11
euler/euler/euler_p4.cpp
Normal file
11
euler/euler/euler_p4.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include "euler_p4.h"
|
||||
#include <string>
|
||||
|
||||
int euler4() {
|
||||
return 1001;
|
||||
}
|
||||
|
||||
bool isPalindrome(const std::string& testString) {
|
||||
|
||||
return true;
|
||||
}
|
13
euler/euler/euler_p4.h
Normal file
13
euler/euler/euler_p4.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
/**
|
||||
* @brief Solves Euler problem 4: largest palindrome
|
||||
*
|
||||
* This function calculates the largest palindrome that is the result of two numbers
|
||||
*
|
||||
* @param limitA one of the numbers to use in the multiplication
|
||||
* @param limitB one of the numbers to use in the multiplication
|
||||
* @return The highest palindrome resulting from a combination a & b
|
||||
*
|
||||
* @note This function only works with positive integer inputs.
|
||||
*/
|
||||
int euler4(int limitA, int limitB);
|
Loading…
x
Reference in New Issue
Block a user