diff --git a/README.md b/README.md index ae8be6c..dce94f8 100644 --- a/README.md +++ b/README.md @@ -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 : \ No newline at end of file diff --git a/euler/euler/euler.cpp b/euler/euler/euler.cpp index 83da8ab..7e8ea91 100644 --- a/euler/euler/euler.cpp +++ b/euler/euler/euler.cpp @@ -5,6 +5,8 @@ #include #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; } diff --git a/euler/euler/euler.vcxproj b/euler/euler/euler.vcxproj index e3aaede..050b672 100644 --- a/euler/euler/euler.vcxproj +++ b/euler/euler/euler.vcxproj @@ -130,10 +130,14 @@ + + + + diff --git a/euler/euler/euler.vcxproj.filters b/euler/euler/euler.vcxproj.filters index bc124d2..8f6cb62 100644 --- a/euler/euler/euler.vcxproj.filters +++ b/euler/euler/euler.vcxproj.filters @@ -24,6 +24,12 @@ Source Files + + Source Files + + + Source Files + @@ -32,5 +38,11 @@ Source Files + + Source Files + + + Source Files + \ No newline at end of file diff --git a/euler/euler/euler_p2.h b/euler/euler/euler_p2.h index b1c4915..49de0b2 100644 --- a/euler/euler/euler_p2.h +++ b/euler/euler/euler_p2.h @@ -1,5 +1,4 @@ #pragma once -#pragma once /** * @brief Solves Euler problem 2: Even Fibonacci Numbers * diff --git a/euler/euler/euler_p3.cpp b/euler/euler/euler_p3.cpp new file mode 100644 index 0000000..310ffcd --- /dev/null +++ b/euler/euler/euler_p3.cpp @@ -0,0 +1,30 @@ +#include "euler_p3.h" +#include + + +/* +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; +} \ No newline at end of file diff --git a/euler/euler/euler_p3.h b/euler/euler/euler_p3.h new file mode 100644 index 0000000..d506839 --- /dev/null +++ b/euler/euler/euler_p3.h @@ -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); \ No newline at end of file diff --git a/euler/euler/euler_p4.cpp b/euler/euler/euler_p4.cpp new file mode 100644 index 0000000..b1ebc93 --- /dev/null +++ b/euler/euler/euler_p4.cpp @@ -0,0 +1,11 @@ +#include "euler_p4.h" +#include + +int euler4() { + return 1001; +} + +bool isPalindrome(const std::string& testString) { + + return true; +} \ No newline at end of file diff --git a/euler/euler/euler_p4.h b/euler/euler/euler_p4.h new file mode 100644 index 0000000..1624b92 --- /dev/null +++ b/euler/euler/euler_p4.h @@ -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); \ No newline at end of file