Skip to content

Commit 2863fa1

Browse files
📦 NEW: Project Euler Problem 5 improvement and added test cases
1 parent 96224e7 commit 2863fa1

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

Diff for: Project-Euler/Problem005.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ Smallest multiple
55
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
66
*/
77

8-
export const findSmallestMultiple = () => {
9-
const divisors = [
10-
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2
11-
]
12-
let num = 21
8+
export const findSmallestMultiple = (maxDivisor) => {
9+
if (isNaN(maxDivisor)) {
10+
return 0
11+
}
12+
const divisors = Array.from({ length: maxDivisor }, (_, i) => i + 1)
13+
let num = maxDivisor + 1
1314
let result
1415

1516
while (!result) {

Diff for: Project-Euler/test/Problem005.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { findSmallestMultiple } from '../Problem005.js'
2+
3+
describe.concurrent('Find smallest multiple', () => {
4+
test('if max divisor is 10', () => {
5+
expect(findSmallestMultiple(10)).toBe(2520)
6+
})
7+
test('if max divisor is 15', () => {
8+
expect(findSmallestMultiple(15)).toBe(360360)
9+
})
10+
// Project Euler Condition Check
11+
test('if max divisor is 20', () => {
12+
expect(findSmallestMultiple(20)).toBe(232792560)
13+
})
14+
})

0 commit comments

Comments
 (0)