Skip to content

Commit a53bfbe

Browse files
authored
Merge pull request #41 from sir-gon/feature/euler001
[Hacker Rank]: Project Euler #1: Multiples of 3 and 5 solved ✓
2 parents 659bf79 + 25e315d commit a53bfbe

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# [Multiples of 3 and 5](https://www.hackerrank.com/contests/projecteuler/challenges/euler001)
2+
3+
- Difficulty: #easy
4+
- Category: #ProjectEuler+
5+
6+
If we list all the natural numbers below $ 10 $ that are multiples of
7+
$ 3 $ or $ 5 $, we get $ 3, 5, 6 $ and $ 9 $.
8+
The sum of these multiples
9+
is $ 23 $.
10+
11+
Find the sum of all the multiples of $ 3 $ or $ 5 $ below $ N $.
12+
13+
## Input Format
14+
15+
First line contains $ T $ that denotes the number of test cases.
16+
This is followed by $ T $ lines, each containing an integer, $ N $.
17+
18+
## Constraints
19+
20+
- 1 $ \leq T \leq 10^5 $
21+
- 1 $ \leq N \leq 10^9 $
22+
23+
## Output Format
24+
25+
For each test case, print an integer that denotes the sum of all the multiples
26+
of $ 3 $ or $ 5 $ below $ N $.
27+
28+
## Sample Input 0
29+
30+
```text
31+
2
32+
10
33+
100
34+
```
35+
36+
## Sample Output 0
37+
38+
```text
39+
23
40+
2318
41+
```
42+
43+
## Explanation 0
44+
45+
For $ N = 10 $, if we list all the natural numbers below $ 10 $ that are
46+
multiples of $ 3 $ or $ 5 $, we get $ 3, 5, 6 $ and $ 9 $.
47+
The sum of these multiples is $ 23 $.
48+
49+
Similarly for $ N = 100 $, we get $ 2318 $.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
namespace hackerrank::projecteuler {
4+
5+
int euler001(int a, int b, int n);
6+
7+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <exercises/hackerrank/projecteuler/euler001.hpp>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]]
5+
*/
6+
7+
#include <algorithm>
8+
9+
namespace hackerrank::projecteuler {
10+
11+
// Function to return gcd of a and b
12+
int gcd(int a, int b) {
13+
// Find Minimum of a and b
14+
int result = std::min(a, b);
15+
while (result > 0) {
16+
if (a % result == 0 && b % result == 0) {
17+
break;
18+
}
19+
result--;
20+
}
21+
22+
// Return gcd of a and b
23+
return result;
24+
}
25+
26+
// Function to find sum of Arithmetic Progression series
27+
int sum_of_arithmetic_progression(int n, int d) {
28+
// Number of terms
29+
n = n / d;
30+
return n * (1 + n) * d / 2;
31+
}
32+
33+
// Function to find the sum of all multiples of a and b below n
34+
int euler001(int a, int b, int n) {
35+
// Since, we need the sum of multiples less than N
36+
n = n - 1;
37+
38+
int lcm = (a * b) / gcd(a, b);
39+
40+
return sum_of_arithmetic_progression(n, a) +
41+
sum_of_arithmetic_progression(n, b) -
42+
sum_of_arithmetic_progression(n, lcm);
43+
}
44+
45+
} // namespace hackerrank::projecteuler
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/projecteuler/euler001.hpp>
4+
#include <iostream>
5+
#include <vector>
6+
7+
#include <filesystem>
8+
#include <fstream>
9+
#include <nlohmann/json.hpp>
10+
using json = nlohmann::json;
11+
12+
TEST_CASE("euler001 JSON Test Cases",
13+
"[hackerrank] [jsontestcase] [projecteuler]") {
14+
std::filesystem::path cwd = std::filesystem::current_path();
15+
std::string path =
16+
cwd.string() +
17+
"/unit/lib/hackerrank/projecteuler/euler001.testcases.json";
18+
19+
INFO("euler001 JSON test cases FILE: " << path);
20+
21+
std::ifstream f(path);
22+
json data = json::parse(f);
23+
24+
for (auto testcase : data) {
25+
int result = hackerrank::projecteuler::euler001(
26+
testcase["a"], testcase["b"], testcase["n"]);
27+
CHECK(result == testcase["expected"]);
28+
}
29+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{ "a": 3, "b": 5, "n": 10, "expected": 23 },
3+
{ "a": 3, "b": 5, "n": 100, "expected": 2318 },
4+
{ "a": 3, "b": 5, "n": 1000, "expected": 233168 }
5+
]

0 commit comments

Comments
 (0)