From 358add901081c3def773a16bf6ba827d974ca73a Mon Sep 17 00:00:00 2001 From: delano zuurman Date: Wed, 12 Feb 2025 23:34:43 +0100 Subject: [PATCH] euler problem 1 solved --- README.md | 7 ++++++- euler/euler/euler.cpp | 30 +++++++++++++++++++----------- euler/euler/euler.vcxproj | 4 ++++ euler/euler/euler.vcxproj.filters | 8 ++++++++ euler/euler/euler_p1.cpp | 19 +++++++++++++++++++ euler/euler/euler_p1.h | 12 ++++++++++++ 6 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 euler/euler/euler_p1.cpp create mode 100644 euler/euler/euler_p1.h diff --git a/README.md b/README.md index d224c31..081651b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # project-euler -Several project euler problems solved in C++ \ No newline at end of file +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 diff --git a/euler/euler/euler.cpp b/euler/euler/euler.cpp index 637c42d..62db2f0 100644 --- a/euler/euler/euler.cpp +++ b/euler/euler/euler.cpp @@ -2,19 +2,27 @@ // #include +#include +#include "euler_p1.h" + + + +void unit_tests() { + + assert(euler1(10) == 23); + + std::cout << "All tests passed! continues main." << std::endl; +} + +int euler_p1() { + int res = euler1(1000); + return res; +} int main() { - std::cout << "Hello World!\n"; + unit_tests(); + + std::cout << "the result for problem 1 = " << euler_p1() << std::endl; } -// Run program: Ctrl + F5 or Debug > Start Without Debugging menu -// Debug program: F5 or Debug > Start Debugging menu - -// Tips for Getting Started: -// 1. Use the Solution Explorer window to add/manage files -// 2. Use the Team Explorer window to connect to source control -// 3. Use the Output window to see build output and other messages -// 4. Use the Error List window to view errors -// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project -// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file diff --git a/euler/euler/euler.vcxproj b/euler/euler/euler.vcxproj index 6ff586c..6ab2224 100644 --- a/euler/euler/euler.vcxproj +++ b/euler/euler/euler.vcxproj @@ -128,6 +128,10 @@ + + + + diff --git a/euler/euler/euler.vcxproj.filters b/euler/euler/euler.vcxproj.filters index 4d134af..104f92e 100644 --- a/euler/euler/euler.vcxproj.filters +++ b/euler/euler/euler.vcxproj.filters @@ -18,5 +18,13 @@ Source Files + + Source Files + + + + + Source Files + \ No newline at end of file diff --git a/euler/euler/euler_p1.cpp b/euler/euler/euler_p1.cpp new file mode 100644 index 0000000..b5c3660 --- /dev/null +++ b/euler/euler/euler_p1.cpp @@ -0,0 +1,19 @@ +#include "euler_p1.h" + +/* +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. + +*/ + +int euler1(int limit) { + int result = 0; + for (size_t i = 1; i < limit; i++) + { + if (i % 3 == 0 || i % 5 == 0) + { + result += i; + } + } + return result; +} \ No newline at end of file diff --git a/euler/euler/euler_p1.h b/euler/euler/euler_p1.h new file mode 100644 index 0000000..a4be6de --- /dev/null +++ b/euler/euler/euler_p1.h @@ -0,0 +1,12 @@ +#pragma once +/** + * @brief Solves Euler problem 1: Multiples of 3 and 5 + * + * This function calculates the sum of all the multiples of 3 or 5 below a given number. + * + * @param limit The upper limit for the range to consider (should be positive). + * @return The sum of multiples of 3 or 5 below the given limit. + * + * @note This function only works with positive integer inputs. + */ +int euler1(int limit); \ No newline at end of file -- 2.43.0