-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from sir-gon/feature/euler001
[Hacker Rank]: Project Euler #1: Multiples of 3 and 5 solved ✓
- Loading branch information
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# [Multiples of 3 and 5](https://www.hackerrank.com/contests/projecteuler/challenges/euler001) | ||
|
||
- Difficulty: #easy | ||
- Category: #ProjectEuler+ | ||
|
||
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 $ N $. | ||
|
||
## Input Format | ||
|
||
First line contains $ T $ that denotes the number of test cases. | ||
This is followed by $ T $ lines, each containing an integer, $ N $. | ||
|
||
## Constraints | ||
|
||
- 1 $ \leq T \leq 10^5 $ | ||
- 1 $ \leq N \leq 10^9 $ | ||
|
||
## Output Format | ||
|
||
For each test case, print an integer that denotes the sum of all the multiples | ||
of $ 3 $ or $ 5 $ below $ N $. | ||
|
||
## Sample Input 0 | ||
|
||
```text | ||
2 | ||
10 | ||
100 | ||
``` | ||
|
||
## Sample Output 0 | ||
|
||
```text | ||
23 | ||
2318 | ||
``` | ||
|
||
## Explanation 0 | ||
|
||
For $ N = 10 $, 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 $. | ||
|
||
Similarly for $ N = 100 $, we get $ 2318 $. |
7 changes: 7 additions & 0 deletions
7
src/lib/exercises/include/exercises/hackerrank/projecteuler/euler001.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
namespace hackerrank::projecteuler { | ||
|
||
int euler001(int a, int b, int n); | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/lib/exercises/src/hackerrank/projecteuler/euler001.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <exercises/hackerrank/projecteuler/euler001.hpp> | ||
|
||
/** | ||
* @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]] | ||
*/ | ||
|
||
#include <algorithm> | ||
|
||
namespace hackerrank::projecteuler { | ||
|
||
// Function to return gcd of a and b | ||
int gcd(int a, int b) { | ||
// Find Minimum of a and b | ||
int result = std::min(a, b); | ||
while (result > 0) { | ||
if (a % result == 0 && b % result == 0) { | ||
break; | ||
} | ||
result--; | ||
} | ||
|
||
// Return gcd of a and b | ||
return result; | ||
} | ||
|
||
// Function to find sum of Arithmetic Progression series | ||
int sum_of_arithmetic_progression(int n, int d) { | ||
// Number of terms | ||
n = n / d; | ||
return n * (1 + n) * d / 2; | ||
} | ||
|
||
// Function to find the sum of all multiples of a and b below n | ||
int euler001(int a, int b, int n) { | ||
// Since, we need the sum of multiples less than N | ||
n = n - 1; | ||
|
||
int lcm = (a * b) / gcd(a, b); | ||
|
||
return sum_of_arithmetic_progression(n, a) + | ||
sum_of_arithmetic_progression(n, b) - | ||
sum_of_arithmetic_progression(n, lcm); | ||
} | ||
|
||
} // namespace hackerrank::projecteuler |
29 changes: 29 additions & 0 deletions
29
src/tests/unit/lib/hackerrank/projecteuler/euler001.test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <exercises/hackerrank/projecteuler/euler001.hpp> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
#include <filesystem> | ||
#include <fstream> | ||
#include <nlohmann/json.hpp> | ||
using json = nlohmann::json; | ||
|
||
TEST_CASE("euler001 JSON Test Cases", | ||
"[hackerrank] [jsontestcase] [projecteuler]") { | ||
std::filesystem::path cwd = std::filesystem::current_path(); | ||
std::string path = | ||
cwd.string() + | ||
"/unit/lib/hackerrank/projecteuler/euler001.testcases.json"; | ||
|
||
INFO("euler001 JSON test cases FILE: " << path); | ||
|
||
std::ifstream f(path); | ||
json data = json::parse(f); | ||
|
||
for (auto testcase : data) { | ||
int result = hackerrank::projecteuler::euler001( | ||
testcase["a"], testcase["b"], testcase["n"]); | ||
CHECK(result == testcase["expected"]); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/tests/unit/lib/hackerrank/projecteuler/euler001.testcases.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
{ "a": 3, "b": 5, "n": 10, "expected": 23 }, | ||
{ "a": 3, "b": 5, "n": 100, "expected": 2318 }, | ||
{ "a": 3, "b": 5, "n": 1000, "expected": 233168 } | ||
] |