add solution 3 and start of 4
This commit is contained in:
parent
d0f9f0ede3
commit
a5c9415d2a
@ -13,3 +13,5 @@ Find the sum of all the multiples of 3 or 5 below 1000.
|
||||
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 :
|
@ -5,6 +5,8 @@
|
||||
#include <cassert>
|
||||
#include "euler_p1.h"
|
||||
#include "euler_p2.h"
|
||||
#include "euler_p3.h"
|
||||
#include "euler_p4.h"
|
||||
|
||||
|
||||
|
||||
@ -12,6 +14,8 @@ 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;
|
||||
}
|
||||
@ -26,11 +30,21 @@ int euler_p2() {
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -130,10 +130,14 @@
|
||||
<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">
|
||||
|
@ -24,6 +24,12 @@
|
||||
<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">
|
||||
@ -32,5 +38,11 @@
|
||||
<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>
|
@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#pragma once
|
||||
/**
|
||||
* @brief Solves Euler problem 2: Even Fibonacci Numbers
|
||||
*
|
||||
|
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