Smallest number that is divisible by first n numbers in JavaScript


In the above problem statement our task is to find the smallest number that is divisible by the first n numbers with the help of Javascript functionalities. So

Understanding the Problem

The problem at hand is to find the smallest number which is evenly divisible by the first n numbers. Or we can say that we have to look for a lowest number that can be divided by each of the numbers from 1 to n without any remainder.

Logic for the given Problem

For solving this problem we can use the concept of Least Common Multiple (LCM). As you may know, the LCM of two numbers is the smallest multiple which is divisible by both the numbers. So by using this concept we can ultimately find the smallest number divisible by all the numbers from 1 to n. To do this task in Javascript we can code a function which can iteratively compute the LCM. So we will start with an initial value of 1 and multiply it by the LCM of every subsequent number. So with the help of it we will continuously update the value of the result until we reach the last number in the range.

Algorithm

Step 1: As we have to find the smallest number that is divisible by first n numbers. So for doing this task we will define a function called smallestDivisibleByN and pass a parameter of n.

Step 2: Inside the above function we will declare a variable called result and initiate its value with 1. After that we will calculate the LCM iteratively with the help of a for loop. Inside this loop calculate the lcm and store its value to the result variable.

Step 3: Now we have defined the lcm function in the above step, in this step we will create this function with name lcm and pass two parameters a and b. Inside this function we will return the value of (a * b) / gcd(a, b).

Step 4: As we have used gcd function above so its time to define that function also. So calculate the greatest common divisor GCD of two numbers. Create a function gcd and pass two parameters a and b.

Step 5: Inside this function we will check that the value of b is equal to zero. If the condition is true then return the value of a. Otherwise return the value of gcd(b, a % b).

Example

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);
}

console.log(smallestDivisibleByN(10));

Output

2520

Complexity

The time complexity for finding the smallest number that is divisible by the first n number is O(n log n), in which n is the provided number as input. The function we have created traverses from 2 to n for calculating the LCM. And the space complexity of the function is O(1) because we have used a constant amount of extra space to store the intermediate results.

Conclusion

So we have successfully coded the problem of finding the smallest number divisible by the first n numbers with the concept of the LCM. The code has O(n log n) time complexity which is making the code suitable for larger values of n.

Updated on: 16-Aug-2023

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements