# Compositorial of a number

Given a natural number N, the task is to find the Nth compositorial number.

Compositorial of a number refers to the product of all the positive composite integers up to N.
The compositorial of a number N is denoted by where N! is the factorial of the number and N# is the Primorial of the number N.

Examples:

Input: N = 4
Output: 1728
Explanation:
The first 4 composite numbers are 4, 6, 8, 9. Therefore, the compositorial is the product of all of the numbers.

Input: N = 5
Output: 17280

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The following steps can be followed to compute the Nth compositorial number.

1. Get the number N.
2. Find all the composite numbers upto N.
3. Product the obtained composite numbers.
4. Print the product.

Below is the implementation of the above approach:

## C++

 // C++ program to find compositorial  // of composite numbers  #include  using namespace std;     vector<int> compo;     // Function to check if  // a number is composite.  bool isComposite(int n)  {             // Corner cases      if (n <= 3)          return false;         // This is checked so that we can      // skip the middle five numbers      // in the below loop      if (n % 2 == 0 or n % 3 == 0)          return true;         int i = 5;      while(i * i <= n)      {          if (n % i == 0 or              n % (i + 2) == 0)              return true;          i = i + 6;      }      return false;  }     // This function stores all  // composite numbers less than N  void Compositorial_list(int n)  {      int l = 0;      for(int i = 4; i < 1000000; i++)      {         if (l < n)         {             if (isComposite(i))             {                 compo.push_back(i);                 l += 1;             }         }      }  }     // Function to calculate   // the compositorial of n  int calculateCompositorial(int n)  {             // Multiply first n composite number      int result = 1;             for(int i = 0; i < n; i++)          result = result * compo[i];      return result;  }     // Driver code  int main()  {      int n = 5;             // Vector to store all the      // composite less than N      Compositorial_list(n);             cout << (calculateCompositorial(n));             return 0;  }     // This code is contributed by mohit kumar 29

## Python3

 # Python3 program to find Compositorial   # of composite numbers        # Function to check   # if a number is composite.   def isComposite(n):               # Corner cases       if (n <= 3):           return False           # This is checked so that we can       # skip the middle five numbers       # in the below loop       if (n % 2 == 0 or n % 3 == 0):           return True        i = 5     while(i * i <= n):                         if (n % i == 0\              or n % (i + 2) == 0):               return True         i = i + 6                   return False         # This function stores all    # Composite numbers less than N  def Compositorial_list(n):      l = 0     for i in range(4, 10**6):          if l

Output:

17280
`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : mohit kumar 29

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.geeksforgeeks.org/compositorial-of-a-number/?ref=leftbar-rightbar", "fetch_time": 1597186485000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2020-34/segments/1596439738855.80/warc/CC-MAIN-20200811205740-20200811235740-00240.warc.gz", "warc_record_offset": 685409585, "warc_record_length": 24410, "token_count": 1141, "char_count": 3907, "score": 3.6875, "int_score": 4, "crawl": "CC-MAIN-2020-34", "snapshot_type": "latest", "language": "en", "language_score": 0.472626268863678, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00014-of-00064.parquet"}