euler problem 1 solved #1
@ -1,3 +1,8 @@
|
|||||||
# project-euler
|
# project-euler
|
||||||
|
|
||||||
Several project euler problems solved in C++
|
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>
|
@ -2,19 +2,27 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <cassert>
|
||||||
|
#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()
|
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
|
|
||||||
|
@ -128,6 +128,10 @@
|
|||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="euler.cpp" />
|
<ClCompile Include="euler.cpp" />
|
||||||
|
<ClCompile Include="euler_p1.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="euler_p1.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
@ -18,5 +18,13 @@
|
|||||||
<ClCompile Include="euler.cpp">
|
<ClCompile Include="euler.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="euler_p1.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="euler_p1.h">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
19
euler/euler/euler_p1.cpp
Normal file
19
euler/euler/euler_p1.cpp
Normal file
@ -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;
|
||||||
|
}
|
12
euler/euler/euler_p1.h
Normal file
12
euler/euler/euler_p1.h
Normal file
@ -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);
|
Loading…
x
Reference in New Issue
Block a user