LCM of First n Natural Numbers

Last Updated : 3 Jun, 2026

Given a number n, find an integer denoting the smallest number divisible by each number from 1 to n.

Examples: 

Input: n = 3
Output: 6
Explanation: 6 is the smallest number divisible by 1, 2 and 3.

Input: n = 6
Output: 60
Explanation: 60 is the smallest number divisible by all from 1 to 6.

Try It Yourself
redirect icon

Using LCM of Every Number - O(n log n) Time O(log n) Space

The idea is to start with res = 1 and iteratively compute the LCM of res and every number from 1 to n. We use: LCM(a, b) = (a * b) / GCD(a, b), After processing all numbers, res becomes the LCM of all numbers from 1 to n.

C++
#include <iostream>
#include <vector>
using namespace std;

// Function to find GCD
long long gcd(long long a, long long b)
{
    if (b == 0)
        return a;

    return gcd(b, a % b);
}

long long getSmallestDivNum(long long n)
{

    long long res = 1;

    // Find LCM of all numbers from 1 to n
    for (long long i = 1; i <= n; i++)
    {
        res = (res * i) / gcd(res, i);
    }

    return res;
}

// Driver code
int main()
{
    long long n = 6;

    cout << getSmallestDivNum(n);

    return 0;
}
Java
import java.util.*;

public class GfG {
    // Function to find GCD
    public static long gcd(long a, long b)
    {
        if (b == 0)
            return a;

        return gcd(b, a % b);
    }

    public static long getSmallestDivNum(long n)
    {

        long res = 1;

        // Find LCM of all numbers from 1 to n
        for (long i = 1; i <= n; i++) {
            res = (res * i) / gcd(res, i);
        }

        return res;
    }

    // Driver code
    public static void main(String[] args)
    {
        long n = 6;

        System.out.println(getSmallestDivNum(n));
    }
}
Python
def gcd(a, b):
    if b == 0:
        return a

    return gcd(b, a % b)


def getSmallestDivNum(n):

    res = 1

    # Find LCM of all numbers from 1 to n
    for i in range(1, n + 1):
        res = (res * i) // gcd(res, i)

    return res


# Driver code
if __name__ == '__main__':
    n = 6

    print(getSmallestDivNum(n))
C#
using System;

public class GfG {
    // Function to find GCD
    public static long gcd(long a, long b)
    {
        if (b == 0)
            return a;

        return gcd(b, a % b);
    }

    public static long getSmallestDivNum(long n)
    {

        long res = 1;

        // Find LCM of all numbers from 1 to n
        for (long i = 1; i <= n; i++) {
            res = (res * i) / gcd(res, i);
        }

        return res;
    }

    // Driver code
    public static void Main()
    {
        long n = 6;

        Console.WriteLine(getSmallestDivNum(n));
    }
}
JavaScript
function gcd(a, b)
{
    if (b === 0)
        return a;

    return gcd(b, a % b);
}

function getSmallestDivNum(n)
{

    let res = 1;

    // Find LCM of all numbers from 1 to n
    for (let i = 1; i <= n; i++) {
        res = (res * i) / gcd(res, i);
    }

    return res;
}

// Driver code
let n = 6;

console.log(getSmallestDivNum(n));

Output
60

Time Complexity: O(n log n)
Auxiliary Space: O(log n)

Using Prime Powers / Sieve - O(n log log n) Time O(n) Space

The idea is to use the fact that the LCM of numbers from 1 to n contains the highest power of every prime that does not exceed n. For every prime p ≤ n:

  • Find the largest power p^k such that p^k ≤ n.
  • Multiply all such powers together.

Let us understand with example:
For n = 6:

  • Highest power of 2 ≤ 6 is 2^2 which is 4
  • Highest power of 3 ≤ 6 is 3*1 which 3
  • Highest power of 5 ≤ 6 is 5*1 which is 5

LCM = 4 × 3 × 5 = 60

C++
#include <iostream>
#include <vector>
using namespace std;

long long getSmallestDivNum(long long n)
{

    vector<bool> isPrime(n + 1, true);

    // Build Sieve of Eratosthenes
    isPrime[0] = false;

    if (n >= 1)
        isPrime[1] = false;

    for (long long i = 2; i * i <= n; i++)
    {
        if (isPrime[i])
        {
            for (long long j = i * i; j <= n; j += i)
            {
                isPrime[j] = false;
            }
        }
    }

    long long lcm = 1;

    // Process every prime
    for (long long i = 2; i <= n; i++)
    {
        if (isPrime[i])
        {
            long long power = i;

            // Find highest power of prime <= n
            while (power <= n / i)
            {
                power *= i;
            }

            lcm *= power;
        }
    }

    return lcm;
}

// Driver code
int main()
{
    long long n = 6;

    cout << getSmallestDivNum(n);

    return 0;
}
Java
import java.util.Arrays;

public class GfG {
    public static long getSmallestDivNum(long n)
    {
        boolean[] isPrime = new boolean[(int)(n + 1)];
        Arrays.fill(isPrime, true);

        // Build Sieve of Eratosthenes
        isPrime[0] = false;

        if (n >= 1)
            isPrime[1] = false;

        for (long i = 2; i * i <= n; i++) {
            if (isPrime[(int)i]) {
                for (long j = i * i; j <= n; j += i) {
                    isPrime[(int)j] = false;
                }
            }
        }

        long lcm = 1;

        // Process every prime
        for (long i = 2; i <= n; i++) {
            if (isPrime[(int)i]) {
                long power = i;

                // Find highest power of prime <= n
                while (power <= n / i) {
                    power *= i;
                }

                lcm *= power;
            }
        }

        return lcm;
    }

    public static void main(String[] args)
    {
        long n = 6;
        System.out.println(getSmallestDivNum(n));
    }
}
Python
def getSmallestDivNum(n):
    isPrime = [True] * (n + 1)

    # Build Sieve of Eratosthenes
    isPrime[0] = False

    if n >= 1:
        isPrime[1] = False

    for i in range(2, int(n**0.5) + 1):
        if isPrime[i]:
            for j in range(i * i, n + 1, i):
                isPrime[j] = False

    lcm = 1

    # Process every prime
    for i in range(2, n + 1):
        if isPrime[i]:
            power = i

            # Find highest power of prime <= n
            while power <= n // i:
                power *= i

            lcm *= power

    return lcm


# Driver code
if __name__ == "__main__":
    n = 6

    print(getSmallestDivNum(n))
C#
using System;

public class GfG {
    public static long getSmallestDivNum(long n)
    {
        bool[] isPrime = new bool[n + 1];
        for (int i = 0; i <= n; i++)
            isPrime[i] = true;

        // Build Sieve of Eratosthenes
        isPrime[0] = false;

        if (n >= 1)
            isPrime[1] = false;

        for (long i = 2; i * i <= n; i++) {
            if (isPrime[i]) {
                for (long j = i * i; j <= n; j += i) {
                    isPrime[j] = false;
                }
            }
        }

        long lcm = 1;

        // Process every prime
        for (long i = 2; i <= n; i++) {
            if (isPrime[i]) {
                long power = i;

                // Find highest power of prime <= n
                while (power <= n / i) {
                    power *= i;
                }

                lcm *= power;
            }
        }

        return lcm;
    }

    public static void Main()
    {
        long n = 6;
        Console.WriteLine(getSmallestDivNum(n));
    }
}
JavaScript
function getSmallestDivNum(n)
{
    let isPrime = Array(n + 1).fill(true);

    // Build Sieve of Eratosthenes
    isPrime[0] = false;

    if (n >= 1)
        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;
            }
        }
    }

    let lcm = 1;

    // Process every prime
    for (let i = 2; i <= n; i++) {
        if (isPrime[i]) {
            let power = i;

            // Find highest power of prime <= n
            while (power <= Math.floor(n / i)) {
                power *= i;
            }

            lcm *= power;
        }
    }

    return lcm;
}

// Driver code
let n = 6;
console.log(getSmallestDivNum(n));

Output
60

Time Complexity: O(n log log n)
Auxiliary Space: O(n)

Comment