Skip to content

Commit 2f43e26

Browse files
authored
Merge pull request #1 from sir-gon/feature/euler001
[Hacker Rank]: Project Euler #1: Multiples of 3 and 5 solved ✓
2 parents b7a4186 + f0b5177 commit 2f43e26

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
unsigned long HACKERRANK_PROJECTEULER_euler001(int a, int b, int n);
8+
9+
#ifdef __cplusplus
10+
} // extern "C"
11+
#endif
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <exercises/hackerrank/projecteuler/euler001.h>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]]
5+
*/
6+
7+
unsigned long HACKERRANK_PROJECTEULER_min(unsigned long a, unsigned long b) {
8+
if (a > b) {
9+
return b;
10+
}
11+
12+
return a;
13+
}
14+
15+
// Function to return gcd of a and b
16+
unsigned long HACKERRANK_PROJECTEULER_gcd(unsigned long a, unsigned long b) {
17+
// Find Minimum of a and b
18+
unsigned long result = HACKERRANK_PROJECTEULER_min(a, b);
19+
while (result > 0) {
20+
if (a % result == 0 && b % result == 0) {
21+
break;
22+
}
23+
result--;
24+
}
25+
26+
// Return gcd of a and b
27+
return result;
28+
}
29+
30+
// Function to find sum of Arithmetic Progression series
31+
unsigned long HACKERRANK_PROJECTEULER_sum_of_arithmetic_progression(unsigned long n, unsigned long d) {
32+
// Number of terms
33+
n = n / d;
34+
return n * (1 + n) * d / 2;
35+
}
36+
37+
// Function to find the sum of all multiples of a and b below n
38+
unsigned long HACKERRANK_PROJECTEULER_euler001(int a, int b, int n) {
39+
// Since, we need the sum of multiples less than N
40+
n = n - 1;
41+
42+
unsigned long lcm = (a * b) / HACKERRANK_PROJECTEULER_gcd(a, b);
43+
44+
return HACKERRANK_PROJECTEULER_sum_of_arithmetic_progression(n, a) +
45+
HACKERRANK_PROJECTEULER_sum_of_arithmetic_progression(n, b) -
46+
HACKERRANK_PROJECTEULER_sum_of_arithmetic_progression(n, lcm);
47+
}
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.h>
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(testcase["a"], testcase["b"],
26+
testcase["n"]);
27+
CHECK(result == testcase["expected"]);
28+
}
29+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{ "a": 3, "b": 5, "n": 10, "expected": 23 },
3+
{ "a": 5, "b": 3, "n": 10, "expected": 23 },
4+
{ "a": 3, "b": 5, "n": 100, "expected": 2318 },
5+
{ "a": 3, "b": 5, "n": 1000, "expected": 233168 }
6+
]

0 commit comments

Comments
 (0)