Skip to content

Commit e93f757

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Project Euler #1: Multiples of 3 and 5 solved ✓
1 parent 3f5ba45 commit e93f757

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]]
3+
*/
4+
5+
// Function to return gcd of a and b
6+
export function gcd(a, b) {
7+
if (a === 0) return b;
8+
return gcd(b % a, a);
9+
}
10+
11+
export function sumAp(n, d) {
12+
// Number of terms
13+
const _n = Math.floor(n / d);
14+
15+
return Math.floor((_n * (1 + _n) * d) / 2);
16+
}
17+
18+
// Function to find the sum of all multiples of a and b below n
19+
export function euler001(a, b, n) {
20+
// Since, we need the sum of multiples less than N
21+
const _n = n - 1;
22+
const lcm = Math.floor((a * b) / gcd(a, b));
23+
24+
return sumAp(_n, a) + sumAp(_n, b) - sumAp(_n, lcm);
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../logger.js';
3+
4+
import { euler001 } from './euler001.js';
5+
6+
import TEST_CASES from './euler001.testcases.json';
7+
8+
describe('euler001', () => {
9+
it('euler001 JSON Test Cases', () => {
10+
expect.assertions(3);
11+
12+
TEST_CASES.forEach((test) => {
13+
const calculated = euler001(test.a, test.b, test.n);
14+
console.log(
15+
`euler001(${test.a}, ${test.b}, ${test.n}) solution found: ${test.answer}`
16+
);
17+
18+
expect(calculated).toStrictEqual(test.answer);
19+
});
20+
});
21+
});
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, "answer": 23 },
3+
{ "a": 3, "b": 5, "n": 100, "answer": 2318 },
4+
{ "a": 3, "b": 5, "n": 1000, "answer": 233168 }
5+
]

0 commit comments

Comments
 (0)