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 bywhere 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
Approach: The following steps can be followed to compute the Nth compositorial number.
- Get the number N.
- Find all the composite numbers upto N.
- Product the obtained composite numbers.
- Print the product.
Below is the implementation of the above approach:
C++
// C++ program to find compositorial // of composite numbers #include <bits/stdc++.h> 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<n: if isComposite(i): compo.append(i) l+= 1 # Function to calculate the # Compositorial of n def calculateCompositorial(n): # Multiply first n composite number result = 1 for i in range(n): result = result * compo[i] return result # Driver code if __name__ == "__main__": n = 5 # Vector to store all the # composite less than N compo =[] Compositorial_list(n) print(calculateCompositorial(n)) |
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.
Recommended Posts:
- Number of factors of very large number N modulo M where M is any prime number
- Count number of trailing zeros in Binary representation of a number using Bitset
- Minimum number of distinct powers of 2 required to express a given binary number
- Count number of triplets with product equal to given number with duplicates allowed
- Find minimum number to be divided to make a number a perfect square
- Find the largest number smaller than integer N with maximum number of set bits
- Minimum divisor of a number to make the number perfect cube
- Minimum number of swaps required to sort an array of first N number
- Minimum number of swaps required to make a number divisible by 60
- Largest number dividing maximum number of elements in the array
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Find smallest possible Number from a given large Number with same count of digits
- Number of ways to split a binary number such that every part is divisible by 2
- Number of times the largest perfect square number can be subtracted from N
- Find smallest number formed by inverting digits of given number N
- Previous perfect square and cube number smaller than number N
- Number of distinct ways to represent a number as sum of K unique primes
- Find the minimum number to be added to N to make it a prime number
- Smallest number dividing minimum number of elements in the Array
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

