Compositorial of a number
Last Updated :
18 Mar, 2021
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 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 up to N.
- Product the obtained composite numbers.
- Print the product.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > compo;
bool isComposite( int n)
{
if (n <= 3)
return false ;
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 ;
}
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;
}
}
}
}
int calculateCompositorial( int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result = result * compo[i];
return result;
}
int main()
{
int n = 5;
Compositorial_list(n);
cout << (calculateCompositorial(n));
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> compo =
new Vector<Integer>();
static boolean isComposite( int n)
{
if (n <= 3 )
return false ;
if (n % 2 == 0 || n % 3 == 0 )
return true ;
int i = 5 ;
while (i * i <= n)
{
if (n % i == 0 ||
n % (i + 2 ) == 0 )
return true ;
i = i + 6 ;
}
return false ;
}
static void Compositorial_list( int n)
{
int l = 0 ;
for ( int i = 4 ; i < 1000000 ; i++)
{
if (l < n)
{
if (isComposite(i))
{
compo.add(i);
l += 1 ;
}
}
}
}
static int calculateCompositorial( int n)
{
int result = 1 ;
for ( int i = 0 ; i < n; i++)
result = result * compo.get(i);
return result;
}
public static void main(String[] args)
{
int n = 5 ;
Compositorial_list(n);
System.out.print((calculateCompositorial(n)));
}
}
|
Python3
def isComposite(n):
if (n < = 3 ):
return False
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
def Compositorial_list(n):
l = 0
for i in range ( 4 , 10 * * 6 ):
if l<n:
if isComposite(i):
compo.append(i)
l + = 1
def calculateCompositorial(n):
result = 1
for i in range (n):
result = result * compo[i]
return result
if __name__ = = "__main__" :
n = 5
compo = []
Compositorial_list(n)
print (calculateCompositorial(n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > compo =
new List< int >();
static bool isComposite( int n)
{
if (n <= 3)
return false ;
if (n % 2 == 0 || n % 3 == 0)
return true ;
int i = 5;
while (i * i <= n)
{
if (n % i == 0 ||
n % (i + 2) == 0)
return true ;
i = i + 6;
}
return false ;
}
static void Compositorial_list( int n)
{
int l = 0;
for ( int i = 4; i < 1000000; i++)
{
if (l < n)
{
if (isComposite(i))
{
compo.Add(i);
l += 1;
}
}
}
}
static int calculateCompositorial( int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result = result * compo[i];
return result;
}
public static void Main(String[] args)
{
int n = 5;
Compositorial_list(n);
Console.Write((calculateCompositorial(n)));
}
}
|
Javascript
<script>
let compo = [];
function isComposite(n)
{
if (n <= 3)
return false ;
if (n % 2 == 0 || n % 3 == 0)
return true ;
let i = 5;
while (i * i <= n)
{
if (n % i == 0 ||
n % (i + 2) == 0)
return true ;
i = i + 6;
}
return false ;
}
function Compositorial_list(n)
{
let l = 0;
for (let i = 4; i < 1000000; i++)
{
if (l < n)
{
if (isComposite(i))
{
compo.push(i);
l += 1;
}
}
}
}
function calculateCompositorial(n)
{
let result = 1;
for (let i = 0; i < n; i++)
result = result * compo[i];
return result;
}
let n = 5;
Compositorial_list(n);
document.write(calculateCompositorial(n));
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...