Given a number, our task is to find the smallest number that is evenly divisible by the first n numbers without giving any remainder in JavaScript.
Example:
Input: 10
Output: 2520
Explanation:
2520 is smallest number which is completely divisible by numbers from 1 to 10.
Below are the following approaches for finding the Smallest number that is divisible by the first n numbers:
Table of Content
Brute Force Approach
To Find the smallest number divisible by the first n numbers using Brute Force Approach first initialize num with n. Use a while loop to find the smallest divisible number. In the loop, set divisible to true, and iterate from 1 to n, checking if num is divisible by i. If not, set divisible to false and break. If divisible remains true, return num. Otherwise, increment num and continue.
Example: Demonstration of Finding the Smallest number divisible by the first n numbers using the Brute Force Approach.
function smallDivi(n) {
let num = n;
while (true) {
let divisible = true;
for (let i = 1; i <= n; i++) {
if (num % i !== 0) {
divisible = false;
break;
}
}
if (divisible) {
return num;
}
num++;
}
}
console.log(smallDivi(10));
Output
2520
Time Complexity: O(num * n)
Space Complexity: O(1)
Using LCM (Lowest Common Multiple)
In this approach, first implement gcd to find the greatest common divisor using Euclid's algorithm. Implement lcm to calculate the least common multiple as a * b / gcd(a, b). Initialize the answer to 1 and iterate from 2 to n, updating the answer with the least common multiple of the answer and i. After the loop, the answer holds the smallest number divisible by the first n numbers. Return answer.
Example: Demonstration of Finding the the Smallest number that is divisible by first n numbers using LCM (Lowest Common Multiple).
function smallestDivisibleLCM(n) {
function calculateGCD(a, b) {
return b === 0 ? a : calculateGCD(b, a % b);
}
function calculateLCM(a, b) {
return (a * b) / calculateGCD(a, b);
}
// Initialize the result to 1
let answer = 1;
// Iterate through numbers from 2 to n to calculate LCM
for (let i = 2; i <= n; i++) {
answer = calculateLCM(answer, i);
}
return answer;
}
console.log(smallestDivisibleLCM(10));
Output
2520
Time Complexity: O(n * log(n)).
Space Complexity: O(1).
Using Prime Factorization Method:
The Prime Factorization Method calculates the smallest number divisible by the first n numbers by identifying all primes up to n, determining the highest power of each prime that fits within n, and multiplying these highest powers together.
Example: In this example we will follow the above explained approach.
function sieve(n) {
let isPrime = Array(n + 1).fill(true);
isPrime[0] = isPrime[1] = false;
for (let i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (let j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
return isPrime.map((prime, index) => prime ? index : false).filter(Boolean);
}
function highestPowerOfPrimeUnderN(prime, n) {
let power = prime;
while (power * prime <= n) {
power *= prime;
}
return power;
}
function smallestDivisibleByFirstN(n) {
const primes = sieve(n);
let result = 1;
primes.forEach(prime => {
result *= highestPowerOfPrimeUnderN(prime, n);
});
return result;
}
let n = 10;
console.log(`The smallest number that is divisible by the first ${n} numbers is:
${smallestDivisibleByFirstN(n)}`);
Output
The smallest number that is divisible by the first 10 numbers is: 2520
Using Array.from and Recursion
This approach leverages recursion to compute the LCM of a list of numbers. By breaking down the problem into smaller subproblems, it recursively calculates the LCM of two numbers at a time.
Example: In this example, we will use a recursive approach to find the smallest number divisible by the first n numbers.
function calculateGCD(a, b) {
return b === 0 ? a : calculateGCD(b, a % b);
}
function calculateLCM(a, b) {
return (a * b) / calculateGCD(a, b);
}
function recursiveLCM(arr) {
if (arr.length === 2) {
return calculateLCM(arr[0], arr[1]);
} else {
const lastElement = arr.pop();
return calculateLCM(lastElement, recursiveLCM(arr));
}
}
function smallestDivisibleRecursiveLCM(n) {
const numbers = Array.from({ length: n }, (_, i) => i + 1);
return recursiveLCM(numbers);
}
console.log(smallestDivisibleRecursiveLCM(10));
Output
2520