Saturday, October 18, 2025
HomeData Modelling & AIGenerate a list of n consecutive composite numbers (An interesting method)

Generate a list of n consecutive composite numbers (An interesting method)

Given a number n, generate a list of n composite numbers.
Examples: 
 

Input : 5
Output : 122, 123, 124, 125

Input : 10
Output : 3628802, 3628803, 3628804, 3628805, 3628806, 
         3628807, 3628808, 3628809, 3628810

 

The idea here is using the properties of n!   . Since n! = 1\times2\times3....(n-1)\times n   , then numbers 1, 2, 3..... (n-1), n   , all divide n!   . Therefore n!+2   is divisible by 2, n!+3   is divisible by 3 ….. n!+n   is divisible by n. And by above pattern they are consecutive composites.
We find (n+1)!, then we print numbers (n+1)! + 2, (n+1)! + 3, …. (n+1)! + (n + 1).
Below is the implementation of above approach:
 

C++




// CPP program to print n consecutive composite
// numbers.
#include <iostream>
using namespace std;
 
// function to find factorial of given
// number
unsigned long long int factorial(unsigned int n)
{   
    unsigned long long int res = 1;
    for (int i=2; i<=n; i++)
        res *= i;
    return res;
}
 
// Prints n consecutive numbers.
void printNComposite(int n)
{
    unsigned long long int fact = factorial(n+1);
    for (int i = 2; i <= n+1; ++i)
        cout << fact + i << " ";
}
 
// Driver program to test above function
int main()
{
    int n = 4;
    printNComposite(n);
    return 0;
}


Java




// Java program to print n consecutive composite
// numbers
 
class GFG {
 
// function to find factorial of given
// number
    static long factorial(int n) {
        long res = 1;
        for (int i = 2; i <= n; i++) {
            res *= i;
        }
        return res;
    }
 
// Prints n consecutive numbers.
    static void printNComposite(int n) {
        long fact = factorial(n + 1);
        for (int i = 2; i <= n + 1; ++i) {
            System.out.print(fact + i + " ");
        }
    }
 
// Driver program to test above function
    public static void main(String[] args) {
        int n = 4;
        printNComposite(n);
 
    }
}


Python3




# Python3 program to print n consecutive
# composite numbers.
 
# function to find factorial
# of given number
def factorial( n):
 
    res = 1;
    for i in range(2, n + 1):
        res *= i;
    return res;
 
# Prints n consecutive numbers.
def printNComposite(n):
    fact = factorial(n + 1);
    for i in range(2, n + 2):
        print(fact + i, end = " ");
 
# Driver Code
n = 4;
printNComposite(n);
     
# This code is contributed by mits


C#




// C# program to print n consecutive composite
// numbers
using System;
                     
public class Program{
  
// function to find factorial of given
// number
    static long factorial(int n) {
        long res = 1;
        for (int i = 2; i <= n; i++) {
            res *= i;
        }
        return res;
    }
  
// Prints n consecutive numbers.
    static void printNComposite(int n) {
        long fact = factorial(n + 1);
        for (int i = 2; i <= n + 1; ++i) {
            Console.Write(fact + i + " ");
        }
    }
  
// Driver program to test above function
    public static void Main() {
        int n = 4;
        printNComposite(n);
  
    }
}
 
// This code is contributed by Rajput-Ji


PHP




<?php
// PHP program to print n consecutive
// composite numbers.
 
// function to find factorial of given
// number
function factorial( $n)
{
    $res = 1;
    for ($i = 2; $i <= $n; $i++)
        $res *= $i;
    return $res;
}
 
// Prints n consecutive numbers.
function printNComposite(int $n)
{
    $fact = factorial($n + 1);
    for($i = 2; $i <= $n + 1; ++$i)
        echo $fact + $i ," ";
}
 
    // Driver Code
    $n = 4;
    printNComposite($n);
     
// This code is contributed by anuj_67.
?>


Javascript




<script>
 
// JavaScript program to print n consecutive composite
// numbers
 
// function to find factorial of given
// number
    function factorial(n) {
        let res = 1;
        for (let i = 2; i <= n; i++) {
            res *= i;
        }
        return res;
    }
   
// Prints n consecutive numbers.
    function printNComposite(n) {
        let fact = factorial(n + 1);
        for (let i = 2; i <= n + 1; ++i) {
            document.write(fact + i + " ");
        }
    }
 
// Driver code
     
        let n = 4;
        printNComposite(n);
     
    // This code is contributed by code_hunt.
</script>


Output: 

122 123 124 125

 

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

The above solution causes overflow very soon (for small values of n). We can use technique to find factorial of large number to avoid overflow.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS