-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Hacker Rank]: Project Euler #1: Multiples of 3 and 5 solved ✓
- Loading branch information
Gonzalo Diaz
committed
Sep 27, 2024
1 parent
3f5ba45
commit e93f757
Showing
4 changed files
with
100 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 $. |
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,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); | ||
} |
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,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); | ||
}); | ||
}); | ||
}); |
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, "answer": 23 }, | ||
{ "a": 3, "b": 5, "n": 100, "answer": 2318 }, | ||
{ "a": 3, "b": 5, "n": 1000, "answer": 233168 } | ||
] |