Project Euler: #5 - Smallest multiple

Search for a command to run...

No comments yet. Be the first to comment.
In this series, I will be discussing Project Euler Solutions. 'Project Euler' helps in improving problem-solving and programming skills. Remember, there is always a better solution to the problem. 💯
Problem The sum of the squares of the first ten natural numbers is, $$1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 = 385$$ The square of the sum of the first ten natural numbers is, $$(1+2+3+4+5+6+7+8+9+10)^2 = 55^2 = 3025$$ Hence the d...
What is JavaScript? Discover how this quirky yet powerful language runs in browsers, what makes it tick, and how you can start adding it to your sites

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...

Problem 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? Problem Description The problem is pretty self-explanatory. 215 is 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26....

Problem Starting in the top left corner of a 2*2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20*20 grid? Problem Description We are given a...

Problem The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → ...

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?
The Problem is straightforward. The smallest number that is divisible by all the numbers from 1 to 10 is 2520.
We need to find the smallest positive number that is divisible by all the numbers from 1 to 20
To calculate, let's break down all the prime numbers between 2 and 20.
So we will have an array like [2, 3, 5, 7, 11, 13, 17, 19].
Then for each of these prime components, we need to find the maximum number of occurrences in each number. In other words, factorize each number from 2 to 20 and find the number of times a particular prime number occurs.
For example, 12 has 2 occurrence of prime number 2 and 1 occurrence of prime number 3. Below is a chart with the maximum occurrence for each number.

Then you raise each prime number to its maximum occurrence: 2^4, 3^2, 5^1, … and multiply the result: 16 * 9 * 5 * ...
In this calculation, we involve only prime numbers between 1 and 20 because the other numbers are eventually multiples of these prime numbers.
const findPrimeNumbers = (start, end) => {
let primeArray = [];
for (let i = start; i <= end; i++) {
let isPrime = true;
for (let j = 2; j <= Math.ceil(i / 2); j++) {
if ((i % j) === 0) {
isPrime = false;
break;
}
}
if (isPrime === true) {
primeArray.push(i);
}
}
return primeArray;
};
const findFactors = (number) => {
var factorsArray = [];
for (let i = 2; i <= number; i++) {
while ((number % i) === 0) {
factorsArray.push(i);
number /= i;
}
}
return factorsArray;
}
const smallestDivNumber = () => {
const primeArr = findPrimeNumbers(2, 20);
for (let i = 0; i < primeArr.length; i++) {
let maxOccur = 0;
for (let j = 2; j <= 20; j++) {
if (!(primeArr[i] > j)) {
const factArr = findFactors(j);
let countOccur = factArr
.filter(x => x === primeArr[i]).length;
if (countOccur > maxOccurance) maxOccur = countOccur;
}
}
if (maxOccur > 0) primeArr[i] = Math.pow(primeArr[i], maxOccur);
}
return primeArr.reduce((acc, val) => acc * val, 1);
}
console.time("Time");
console.log(smallestDivNumber());
console.timeEnd("Time");
You can find my solution on GitHub 05
If you have another or a better solution, please leave it in the comments below.
For the other Project Euler Solutions, please follow the series Project Euler Solutions in JS.
Thank you!