Skip to content

Commit

Permalink
[Hacker Rank]: Project Euler #1: Multiples of 3 and 5 solved ✓
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonzalo Diaz committed Sep 27, 2024
1 parent 3f5ba45 commit e93f757
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/hackerrank/projecteuler/euler001.md
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 $.
25 changes: 25 additions & 0 deletions src/hackerrank/projecteuler/euler001.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]]
*/

// Function to return gcd of a and b
export function gcd(a, b) {
if (a === 0) return b;
return gcd(b % a, a);
}

export function sumAp(n, d) {
// Number of terms
const _n = Math.floor(n / d);

return Math.floor((_n * (1 + _n) * d) / 2);
}

// Function to find the sum of all multiples of a and b below n
export function euler001(a, b, n) {
// Since, we need the sum of multiples less than N
const _n = n - 1;
const lcm = Math.floor((a * b) / gcd(a, b));

return sumAp(_n, a) + sumAp(_n, b) - sumAp(_n, lcm);
}
21 changes: 21 additions & 0 deletions src/hackerrank/projecteuler/euler001.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../logger.js';

import { euler001 } from './euler001.js';

import TEST_CASES from './euler001.testcases.json';

describe('euler001', () => {
it('euler001 JSON Test Cases', () => {
expect.assertions(3);

TEST_CASES.forEach((test) => {
const calculated = euler001(test.a, test.b, test.n);
console.log(
`euler001(${test.a}, ${test.b}, ${test.n}) solution found: ${test.answer}`
);

expect(calculated).toStrictEqual(test.answer);
});
});
});
5 changes: 5 additions & 0 deletions src/hackerrank/projecteuler/euler001.testcases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{ "a": 3, "b": 5, "n": 10, "answer": 23 },
{ "a": 3, "b": 5, "n": 100, "answer": 2318 },
{ "a": 3, "b": 5, "n": 1000, "answer": 233168 }
]

0 comments on commit e93f757

Please sign in to comment.