VOOZH about

URL: https://www.geeksforgeeks.org/dsa/generate-list-n-consecutive-composite-numbers-interesting-method/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 11 Jul, 2025

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 . Since , then numbers , all divide . Therefore is divisible by 2, is divisible by 3 ..... 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:
 


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.
 

Comment
Article Tags:
Article Tags: