LCM of First n Natural Numbers

Given a Number n such that 1 <= N <= 10^6, the Task is to Find the LCM of First n Natural Numbers.
Examples:

Input : n = 5
Output : 60

Input : n = 6
Output : 60

Input : n = 7
Output : 420

We strongly recommend that you click here and practice it, before moving on to the solution.

We have discussed a simple solution in below article.
Smallest number divisible by first n numbers

The above solution works fine for single input. But if we have multiple inputs, it is a good idea to use Sieve of Eratosthenes to store all prime factors. As we know if LCM(a, b) = X so any prime factor of a or b will also be the prime factor of ‘X’.



  1. Initialize lcm variable with 1
  2. Generate all prime number less then 10^6 and store in Array prime by using Sieve of Eratosthenes.
  3. Find the maximum number which is less than the given number and equal to power of the prime.
  4. Then multiply this number with lcm variable.
  5. Tepeat step 3 and 4 until prime is less than the given number.

Illustration:

For example, if n = 10 
8 will be the first number which is equal to 2^3
then 9 which is equal to 3^2
then 5 which is equal to 5^1
then 7 which is equal to 7^1
Finally, we multiply those numbers 8*9*5*7 = 2520

Below is the implementation of above idea.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find LCM of First N Natural Numbers.
#include <bits/stdc++.h>
#define MAX 100000
using namespace std;
  
// array to store all prime less than and equal to 10^6
vector <int> primes;
  
// utility function for sieve of sieve of Eratosthenes
void sieve()
{
    bool isComposite[MAX] = {false};
    for (int i=2; i*i<=MAX; i++)
    {
        if (isComposite[i]==false)
            for (int j=2; j*i<=MAX; j++)
                isComposite[i*j] = true;
    }
  
    // Store all prime numbers in vector primes[]
    for (int i=2; i<=MAX; i++)
        if (isComposite[i]==false)
            primes.push_back(i);
}
  
// Function to find LCM of first n Natural Numbers
long long LCM(int n)
{
    long long lcm = 1;
    for (int i=0; i<primes.size() && primes[i]<=n; i++)
    {
        // Find the highest power of prime, primes[i]
        // that is less than or equal to n
        int pp = primes[i];
        while (pp*primes[i] <= n)
            pp = pp *primes[i];
  
        // multiply lcm with highest power of prime[i]
        lcm *= pp;
        lcm %= 1000000007;
    }
    return lcm;
}
  
// Driver code
int main()
{
    sieve();
    int N = 7;
    cout << LCM(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find LCM of First N Natural Numbers.
import java.util.*;
  
class GFG{
static int MAX=100000;
  
// array to store all prime less than and equal to 10^6
static ArrayList<Integer> primes = new ArrayList<Integer>();
// utility function for sieve of sieve of Eratosthenes
static void sieve()
{
    boolean[] isComposite=new boolean[MAX+1];
    for (int i=2; i*i<=MAX; i++)
    {
        if (isComposite[i]==false)
            for (int j=2; j*i<=MAX; j++)
                isComposite[i*j] = true;
    }
  
    // Store all prime numbers in vector primes[]
    for (int i=2; i<=MAX; i++)
        if (isComposite[i]==false)
            primes.add(i);
}
  
// Function to find LCM of first n Natural Numbers
static long LCM(int n)
{
    long lcm = 1;
    for (int i=0; i<primes.size() && primes.get(i)<=n; i++)
    {
        // Find the highest power of prime, primes[i]
        // that is less than or equal to n
        int pp = primes.get(i);
        while (pp*primes.get(i) <= n)
            pp = pp *primes.get(i);
  
        // multiply lcm with highest power of prime[i]
        lcm *= pp;
        lcm %= 1000000007;
    }
    return lcm;
}
  
// Driver code
public static void main(String[] args)
{
    sieve();
    int N = 7;
    System.out.println(LCM(N));
}
}
// This code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find LCM of 
# First N Natural Numbers. 
MAX = 100000
  
# array to store all prime less 
# than and equal to 10^6 
primes = []; 
  
# utility function for 
# sieve of Eratosthenes 
def sieve(): 
       
    isComposite = [False]*(MAX+1);
    i = 2;
    while (i * i <= MAX): 
        if (isComposite[i] == False):
            j = 2;
            while (j * i <= MAX):
                isComposite[i * j] = True;
                j+=1;
        i+=1;
  
    # Store all prime numbers in 
    # vector primes[] 
    for i in range(2,MAX+1): 
        if (isComposite[i] == False): 
            primes.append(i);
  
# Function to find LCM of 
# first n Natural Numbers 
def LCM(n): 
   
    lcm = 1;
    i = 0;
    while (i < len(primes) and primes[i] <= n): 
        # Find the highest power of prime, 
        # primes[i] that is less than or 
        # equal to n 
        pp = primes[i]; 
        while (pp * primes[i] <= n): 
            pp = pp * primes[i]; 
  
        # multiply lcm with highest 
        # power of prime[i] 
        lcm *= pp; 
        lcm %= 1000000007;
        i+=1;
    return lcm; 
  
# Driver code 
sieve(); 
N = 7
print(LCM(N)); 
  
# This code is contributed by mits

chevron_right


C#

// C# program to find LCM of First N
// Natural Numbers.
using System.Collections;
using System;

class GFG
{
static int MAX = 100000;

// array to store all prime less than
// and equal to 10^6
static ArrayList primes = new ArrayList();

// utility function for sieve of
// sieve of Eratosthenes
static void sieve()
{
bool[] isComposite = new bool[MAX + 1];
for (int i = 2; i * i <= MAX; i++) { if (isComposite[i] == false) for (int j = 2; j * i <= MAX; j++) isComposite[i * j] = true; } // Store all prime numbers in vector primes[] for (int i = 2; i <= MAX; i++) if (isComposite[i] == false) primes.Add(i); } // Function to find LCM of first // n Natural Numbers static long LCM(int n) { long lcm = 1; for (int i = 0; i < primes.Count && (int)primes[i] <= n; i++) { // Find the highest power of prime, primes[i] // that is less than or equal to n int pp = (int)primes[i]; while (pp*(int)primes[i] <= n) pp = pp*(int)primes[i]; // multiply lcm with highest power of prime[i] lcm *= pp; lcm %= 1000000007; } return lcm; } // Driver code public static void Main() { sieve(); int N = 7; Console.WriteLine(LCM(N)); } } // This code is contributed by mits [tabby title="PHP"]

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find LCM of
// First N Natural Numbers.
$MAX = 100000;
  
// array to store all prime less
// than and equal to 10^6
$primes = array();
  
// utility function for
// sieve of Eratosthenes
function sieve()
{
    global $MAX, $primes;
    $isComposite = array_fill(0, $MAX, false);
    for ($i = 2; $i * $i <= $MAX; $i++)
    {
        if ($isComposite[$i] == false)
            for ($j = 2; $j * $i <= $MAX; $j++)
                $isComposite[$i * $j] = true;
    }
  
    // Store all prime numbers in
    // vector primes[]
    for ($i = 2; $i <= $MAX; $i++)
        if ($isComposite[$i] == false)
            array_push($primes, $i);
}
  
// Function to find LCM of 
// first n Natural Numbers
function LCM($n)
{
    global $MAX, $primes;
    $lcm = 1;
    for ($i = 0; $i < count($primes) && 
                 $primes[$i] <= $n; $i++)
    {
        // Find the highest power of prime, 
        // primes[i] that is less than or 
        // equal to n
        $pp = $primes[$i];
        while ($pp * $primes[$i] <= $n)
            $pp = $pp * $primes[$i];
  
        // multiply lcm with highest
        // power of prime[i]
        $lcm *= $pp;
        $lcm %= 1000000007;
    }
    return $lcm;
}
  
// Driver code
sieve();
$N = 7;
echo LCM($N);
  
// This code is contributed by mits
?>

chevron_right



Output:

420

This article is contributed by Kuldeep Singh (kulli_d_coder). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.