Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Smallest number that is divisible by first n numbers in JavaScript
In this problem, we need to find the smallest number that is divisible by all numbers from 1 to n. This is essentially finding the Least Common Multiple (LCM) of the first n natural numbers.
Understanding the Problem
The problem requires finding the smallest positive integer that is evenly divisible by each of the numbers from 1 to n without any remainder. For example, if n = 4, we need the smallest number divisible by 1, 2, 3, and 4, which is 12.
Logic and Approach
We solve this using the Least Common Multiple (LCM) concept. The LCM of two numbers is the smallest multiple divisible by both numbers. To find the LCM of multiple numbers, we can iteratively compute the LCM of pairs.
The key insight is: LCM(a, b) = (a × b) / GCD(a, b), where GCD is the Greatest Common Divisor.
Algorithm Steps
Step 1: Initialize result = 1
Step 2: For each number i from 2 to n, calculate result = LCM(result, i)
Step 3: Use the formula LCM(a, b) = (a × b) / GCD(a, b)
Step 4: Calculate GCD using Euclidean algorithm
Implementation
function smallestDivisibleByN(n) {
let result = 1;
// Calculate the LCM iteratively
for (let i = 2; i <= n; i++) {
result = lcm(result, i);
}
return result;
}
// Calculate the LCM of two numbers
function lcm(a, b) {
return (a * b) / gcd(a, b);
}
// Calculate the greatest common divisor (GCD) of two numbers
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
// Test with different values
console.log("Smallest number divisible by first 5 numbers:", smallestDivisibleByN(5));
console.log("Smallest number divisible by first 10 numbers:", smallestDivisibleByN(10));
console.log("Smallest number divisible by first 12 numbers:", smallestDivisibleByN(12));
Smallest number divisible by first 5 numbers: 60 Smallest number divisible by first 10 numbers: 2520 Smallest number divisible by first 12 numbers: 27720
How It Works
Let's trace through n = 5:
- Start with result = 1
- i = 2: result = LCM(1, 2) = 2
- i = 3: result = LCM(2, 3) = 6
- i = 4: result = LCM(6, 4) = 12
- i = 5: result = LCM(12, 5) = 60
Alternative Approach: Prime Factorization
function smallestDivisibleByNPrime(n) {
let result = 1;
// For each number from 2 to n
for (let i = 2; i <= n; i++) {
let temp = i;
let factor = 2;
// Find prime factors of current number
while (factor <= temp) {
let count = 0;
while (temp % factor === 0) {
temp /= factor;
count++;
}
// Update result with highest power of this prime
if (count > 0) {
let currentPower = 0;
let resultCopy = result;
while (resultCopy % factor === 0) {
resultCopy /= factor;
currentPower++;
}
if (count > currentPower) {
result *= Math.pow(factor, count - currentPower);
}
}
factor++;
}
}
return result;
}
console.log("Using prime factorization for n=10:", smallestDivisibleByNPrime(10));
Using prime factorization for n=10: 2520
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| LCM Method | O(n log n) | O(1) |
| Prime Factorization | O(n²) | O(1) |
Conclusion
The LCM approach efficiently finds the smallest number divisible by the first n numbers with O(n log n) time complexity. This method works well for reasonable values of n and demonstrates the practical application of mathematical concepts in programming.
