Finding the smallest multiple in JavaScript

In JavaScript, finding the smallest multiple of numbers from 1 to n is equivalent to finding their Least Common Multiple (LCM). This tutorial demonstrates how to solve this problem using loops and mathematical concepts.

Understanding the Problem

The problem is to find the smallest positive number that is divisible by all numbers from 1 to a given number n. For example, the smallest multiple of numbers 1 to 4 is 12, because 12 is divisible by 1, 2, 3, and 4.

Algorithm Logic

Our approach uses a brute-force method: start with the given number and increment until we find a number divisible by all integers from 1 to n. While not the most efficient, it's straightforward to understand and implement.

Step-by-Step Implementation

Step 1: Create a function that takes the target number as input.

Step 2: Initialize a variable multiple with the input number.

Step 3: Use a while loop to check each potential multiple.

Step 4: For each candidate, verify divisibility by all numbers from 1 to n.

Step 5: Return the first number that satisfies all divisibility conditions.

Example

// Function to find the smallest multiple of numbers from 1 to n
function findSmallestMultiple(number) {
    let multiple = number;

    while (true) {
        let isValidMultiple = true;

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

        if (isValidMultiple) {
            return multiple;
        }
        
        multiple++;
    }
}

// Test with different values
console.log("Smallest multiple of 1-4:", findSmallestMultiple(4));
console.log("Smallest multiple of 1-6:", findSmallestMultiple(6));
console.log("Smallest multiple of 1-10:", findSmallestMultiple(10));
console.log("Smallest multiple of 1-12:", findSmallestMultiple(12));
Smallest multiple of 1-4: 12
Smallest multiple of 1-6: 60
Smallest multiple of 1-10: 2520
Smallest multiple of 1-12: 27720

Optimized Approach Using LCM

A more efficient solution uses the mathematical relationship between LCM and GCD (Greatest Common Divisor):

// Helper function to find GCD using Euclidean algorithm
function findGCD(a, b) {
    while (b !== 0) {
        let temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

// Function to find LCM of two numbers
function findLCM(a, b) {
    return (a * b) / findGCD(a, b);
}

// Optimized function to find smallest multiple
function findSmallestMultipleOptimized(n) {
    let result = 1;
    
    for (let i = 2; i <= n; i++) {
        result = findLCM(result, i);
    }
    
    return result;
}

// Compare both approaches
console.log("Brute force result:", findSmallestMultiple(8));
console.log("Optimized result:", findSmallestMultipleOptimized(8));
Brute force result: 840
Optimized result: 840

Complexity Analysis

Approach Time Complexity Space Complexity
Brute Force O(result × n) O(1)
LCM Method O(n × log(max_value)) O(1)

Conclusion

While the brute-force approach is intuitive, the LCM-based solution is significantly more efficient for larger values. Both methods demonstrate important programming concepts: loops, conditionals, and mathematical optimization in JavaScript.

Updated on: 2026-03-15T23:19:00+05:30

546 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements