JavaScript Implementation of Problem 5
View source code here on GitHub!
- p0005()
Project Euler Problem 5
I solved this problem by testing all combinations of the various multiples. I actually tried to solve this by hand before doing this, and it wasn't terribly hard. The answer turns out to be (2**4 * 3**2 * 5 * 7 * 11 * 13 * 17 * 19)
Problem:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
- Returns:
number --
1/**
2 * Project Euler Problem 5
3 *
4 * I solved this problem by testing all combinations of the various multiples. I
5 * actually tried to solve this by hand before doing this, and it wasn't terribly
6 * hard. The answer turns out to be (2**4 * 3**2 * 5 * 7 * 11 * 13 * 17 * 19)
7 *
8 * Problem:
9 *
10 * 2520 is the smallest number that can be divided by each of the numbers from 1
11 * to 10 without any remainder.
12 *
13 * What is the smallest positive number that is evenly divisible by all of the
14 * numbers from 1 to 20?
15 *
16 * @return {number}
17 **/
18exports.p0005 = function() {
19 const group = [...Array(21).keys()].slice(1);
20 let answer = 1000000000000;
21 for (const x of group) {
22 for (const multiples of iters.combinations(group, x)) {
23 const num = multiples.reduce((a, x) => a * x, 1);
24 if (num < answer && group.every((divisor) => num % divisor == 0)) {
25 answer = num;
26 }
27 }
28 }
29 return answer;
30};
31
32const iters = require('./lib/iters.js');