Finding the smallest multiple in JavaScript


In the given problem statement we have to find the smallest multiple of the given numbers with the help of Javascript functionalities. So we will use loops to iterate through and check the multiples of the given numbers.

Understanding the Problem

The problem at hand is to find the smallest multiple of the given number in Javascript. The multiple of a number is a number that can be divided evenly by that number. To solve the problem we will use a loop and iterate over the numbers until we find the smallest multiple which is divisible by all the numbers from 1 to the given number. For example if we have to find the multiple of 4 so the resultant number will be 12 because the number 12 is divisible by 1, 2, 3 and 4.

Logic for the given Problem

In the function we will use a number as a parameter and initialize a variable multiple with the value of number. Then we will use a while loop to iterate over the numbers until a multiple is found which is divisible by all the numbers from 1 to the given number. If the multiple is not satisfying the divisibility condition for any number then we will move to the next multiple by incrementing it. This process will be continued until we will not find a multiple that satisfies the divisibility condition for all the numbers.

Algorithm

Step 1: As we have to find out the smallest multiple of the given number so we will create a function and in this function we will pass a parameter of number as input. This function will find out the multiple of the given number from 1 to the given number.

Step 2: So we will use a variable called multiple in which initially we will store the given input number. Basically we will start finding multiple from the given number itself.

Step 3: Then we will use a while loop and pass the condition true, a boolean value. Inside the loop we will set another variable to check that the multiple is true or false and name it as isMultiple set its initial value as true.

Step 4: Now we will check if the current multiple is divisible by all the numbers starting from 1 to the given number using a for loop. So here we will check divisibility. If the current multiple is not divisible by any number from 1 to the given number the isMultiple variable will be set to false. And the loop breaks here.

Step 5: If the isMultiple remains true after the for loop that means it is the multiple of the given number and it will be returned.

Example

// Function to find the smallest multiple of the given number
function findMultiple(number) {
   // Start with the given number
   let multiple = number;

   while (true) {
      let isMultiple = true;

      // Check if the current multiple is divisible by all numbers from 1 to the given number
      for (let i = 1; i <= number; i++) {
         if (multiple % i !== 0) {
            isMultiple = false;
            break;
         }
      }

      if (isMultiple) {
         // Return the smallest multiple
         return multiple;
      }
      // Increment the multiple if it's not divisible
      multiple++;
   }
}

console.log(findMultiple(4));
console.log(findMultiple(10));
console.log(findMultiple(11));
console.log(findMultiple(15));

Output

12
2520
27720
360360

Complexity

The time complexity of the code depends on the value of the smallest multiple and the given number. In the worst case, the code may need to iterate through the large number of potential multiples before finding the smallest multiple which satisfies the divisibility condition. So the time complexity is O(m * n), in this m is the value of the smallest multiple and n is the given number. The space complexity of the code is O(1) because it uses a few variables to store the values.

Conclusion

In the code we have used basic Javascript functionality to get the required smallest multiple of the given number. This function may not be the most efficient solution in terms of the time complexity. For larger values the time complexity can be high.

Updated on: 14-Aug-2023

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements