A composite number is a positive integer that is not prime. In other words, it has a positive divisor other than one or itself. First few composite numbers are 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, ………
- Every integer greater than one is either a prime number or a composite number.
- The number one is a unit – it is neither prime nor composite.
How to check if a given number is a composite number or not?
Examples:
Input : n = 21
Output: Yes
The number is a composite number!
Input : n = 11
Output : No
The idea is simple, we can use any of the below methods used for prime checking. We just need to change return statements. Return true is changed to return false and vice versa.
In below code optimized school method is discussed.
C++
#include <bits/stdc++.h>
using namespace std;
bool isComposite( int n)
{
if (n <= 1) return false ;
if (n <= 3) return false ;
if (n%2 == 0 || n%3 == 0) return true ;
for ( int i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return true ;
return false ;
}
int main()
{
isComposite(11)? cout << " true\n" : cout << " false\n" ;
isComposite(15)? cout << " true\n" : cout << " false\n" ;
return 0;
}
|
Java
import java.io.*;
class Composite
{
static boolean isComposite( int n)
{
if (n <= 1 )
System.out.println( "False" );
if (n <= 3 )
System.out.println( "False" );
if (n % 2 == 0 || n % 3 == 0 ) return true ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return true ;
return false ;
}
public static void main(String args[])
{
System.out.println(isComposite( 11 ) ?
"true" : "false" );
System.out.println(isComposite( 15 ) ?
"true" : "false" );
}
}
|
Python 3
def isComposite(n):
if (n < = 1 ):
return False
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
print ( "true" ) if (isComposite( 11 )) else print ( "false" )
print ( "true" ) if (isComposite( 15 )) else print ( "false" )
|
C#
using System;
namespace Composite
{
public class GFG
{
public static bool isComposite( int n)
{
if (n <= 1) return false ;
if (n <= 3) return false ;
if (n % 2 == 0 || n % 3 == 0) return true ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return true ;
return false ;
}
public static void Main()
{
if (isComposite(11)) Console.WriteLine( "true" );
else Console.WriteLine( "false" );
if (isComposite(15)) Console.WriteLine( "true" );
else Console.WriteLine( "false" );
}
}
}
|
PHP
<?php
function isComposite( $n )
{
if ( $n <= 1)
return false;
if ( $n <= 3)
return false;
if ( $n %2 == 0 || $n % 3 == 0)
return true;
for ( $i = 5; $i * $i <= $n ;
$i = $i + 6)
if ( $n % $i == 0 || $n % ( $i + 2) == 0)
return true;
return false;
}
if (isComposite(11))
echo "true" ;
else
echo "false" ;
echo "\n" ;
if (isComposite(15))
echo "true" ;
else
echo "false" ;
echo "\n" ;
?>
|
Javascript
<script>
function isComposite(n)
{
if (n <= 1) return false ;
if (n <= 3) return false ;
if (n%2 == 0 || n%3 == 0) return true ;
for (let i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return true ;
return false ;
}
isComposite(11)? document.write( " true" + "<br>" ): document.write( " false" + "<br>" );
isComposite(15)? document.write( " true" + "<br>" ): document.write( " false" + "<br>" );
</script>
|
Output:
false
true
Time Complexity:- O(sqrt(n))
Space Complexity:-O(1)
Program on Composite Numbers
Reference :
https://en.wikipedia.org/wiki/Composite_number
This article is contributed by Ajay Puri. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.