Skip to content

Commit 675fd58

Browse files
committed
feat: use prime factors to speed up Euler 005.
1 parent 08d8c6b commit 675fd58

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

Project-Euler/Problem005.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
1-
/*
2-
Smallest multiple
1+
import { PrimeFactors } from '../Maths/PrimeFactors.js'
32

4-
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
5-
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
6-
*/
3+
/**
4+
* Smallest Multiple
5+
* @link https://projecteuler.net/problem=5
6+
*
7+
* 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
8+
*
9+
* What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
10+
*/
711

8-
export const findSmallestMultiple = (maxDivisor) => {
9-
const divisors = Array.from({ length: maxDivisor }, (_, i) => i + 1)
10-
let num = maxDivisor + 1
11-
let result
12+
export function findSmallestMultiple(maxDivisor) {
13+
const maxPowers = {}
14+
for (let divisor = 2; divisor <= maxDivisor; divisor++) {
15+
const factors = PrimeFactors(divisor)
1216

13-
while (!result) {
14-
const isDivisibleByAll = divisors.every((divisor) => num % divisor === 0)
15-
if (isDivisibleByAll) result = num
16-
else num++
17+
// combine/count prime factors
18+
let powers = {}
19+
for (const factor of factors) {
20+
powers[factor] = (powers[factor] ?? 0) + 1
21+
}
22+
23+
// save largest factors
24+
for (const factor in powers) {
25+
if (powers[factor] > (maxPowers[factor] ?? 0)) {
26+
maxPowers[factor] = powers[factor]
27+
}
28+
}
1729
}
1830

19-
return result
31+
// multiply all primes
32+
return Object.entries(maxPowers).reduce(
33+
(product, [prime, power]) => product * Math.pow(prime, power),
34+
1
35+
)
2036
}

0 commit comments

Comments
 (0)