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++
// A optimized school method based C++ program to check
// if a number is composite.
#include <bits/stdc++.h>
using namespace std;
bool isComposite(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return false;
// This is checked so that we can skip
// middle five numbers in below loop
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;
}
// Driver Program to test above function
int main()
{
isComposite(11)? cout << " true\n": cout << " false\n";
isComposite(15)? cout << " true\n": cout << " false\n";
return 0;
}
Java
/// An optimized method based Java
// program to check if a number
// is Composite or not.
import java.io.*;
class Composite
{
static boolean isComposite(int n)
{
// Corner cases
if (n <= 1)
System.out.println("False");
if (n <= 3)
System.out.println("False");
// This is checked so that we can skip
// middle five numbers in below loop
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;
}
// Driver Program to test above function
public static void main(String args[])
{
System.out.println(isComposite(11) ?
"true" : "false");
System.out.println(isComposite(15) ?
"true" : "false");
}
}
// This code is contributed by Anshika Goyal
Python 3
# A optimized school method based Python program to check
# if a number is composite.
def isComposite(n):
# Corner cases
if (n <= 1):
return False
if (n <= 3):
return False
# This is checked so that we can skip
# middle five numbers in 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
# Driver Program to test above function
print("true") if(isComposite(11)) else print("false")
print("true") if(isComposite(15)) else print("false")
# This code is contributed by Anant Agarwal.
C#
// A optimized school method based C# program
// to check if a number is composite.
using System;
namespace Composite
{
public class GFG
{
public static bool isComposite(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return false;
// This is checked so that we can skip
// middle five numbers in below loop
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;
}
// Driver Code
public static void Main()
{
if(isComposite(11)) Console.WriteLine("true");
else Console.WriteLine("false");
if(isComposite(15)) Console.WriteLine("true");
else Console.WriteLine("false");
}
}
}
// This code is contributed by Sam007
JavaScript
<script>
// A optimized school method based Javascript program to check
// if a number is composite.
function isComposite(n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return false;
// This is checked so that we can skip
// middle five numbers in below loop
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;
}
// Driver Program to test above function
isComposite(11)? document.write(" true" + "<br>"): document.write(" false" + "<br>");
isComposite(15)? document.write(" true" + "<br>"): document.write(" false" + "<br>");
// This code is contributed by Mayank Tyagi
</script>
PHP
<?php
// A optimized school
// method based PHP
// program to check
// if a number is composite.
function isComposite($n)
{
// Corner cases
if ($n <= 1)
return false;
if ($n <= 3)
return false;
// This is checked so
// that we can skip
// middle five numbers
// in below loop
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;
}
// Driver Code
if(isComposite(11))
echo "true";
else
echo "false";
echo"\n";
if(isComposite(15))
echo "true";
else
echo "false";
echo"\n";
// This code is contributed by Ajit.
?>
Output:
false
true
Time Complexity:- O(sqrt(n))
Space Complexity:-O(1)
Program on Composite Numbers
Reference :
https://en.wikipedia.org/wiki/Composite_number
Similar Reads
String Guide for Competitive Programming Strings are a sequence of characters, and are one of the most fundamental data structures in Competitive Programming. String problems are very common in competitive programming contests, and can range from simple to very challenging. In this article we are going to discuss about most frequent string
15 min read
Blog | Programming Guidelines Computer programming is a process of writing an executable computer program for accomplishing a specific computer task. Although writing the programs is an art, however, there should be some minimum guidelines that should be followed while writing programs. Name: Define the variable names as per pro
2 min read
C/C++ Tricky Programs We may come across various tricky programs in our day-to-day life. Maybe in technical interviews, coding tests, or C/C++ classrooms. Here is a list of such programs:- Print text within double quotes (" "). This may seem easy, but beginners may get puzzled while printing text within double quotes. C
6 min read
C++ Tutorial | Learn C++ Programming C++ is a popular programming language that was developed as an extension of the C programming language to include OOPs programming paradigm. Since then, it has become foundation of many modern technologies like game engines, web browsers, operating systems, financial systems, etc.Features of C++Why
5 min read
Competitive Programming (CP) Handbook with Complete Roadmap Welcome to the Competitive Programming Handbook or CP Handbook by GeeksforGeeks! This Competitive Programming Handbook is a go-to resource for individuals aiming to enhance their problem-solving skills and excel in coding competitions. This CP handbook provides a comprehensive guide, covering fundam
12 min read
Print "GeeksforGeeks" in 10 different programming languages The most elementary part of learning any computer programming language is the ability to print a desired text on the screen or console. Thus, the task of this article is to guide programmers new to any of the 10 different languages discussed below, i.e. GO, Fortran, Pascal, Scala, Perl, ADA, Ruby, K
4 min read
String Manipulation Techniques Recent articles on String ! Word Wrap problem ( Space optimized solution ) Form minimum number from given sequence Maximum number of characters between any two same character in a string Print shortest path to print a string on screen Minimum number of stops from given path Check whether second stri
6 min read
Introduction to Processing | Java Processing is an open-source programming language and development environment that is built on top of the Java programming language. It is specifically designed for artists, designers, and other creative professionals who want to create interactive graphics, animations, and other visual applications
6 min read
How to Create an Application Boom Virus? The virus that we are building here will automatically open up random applications of the PC until the PC freezes. This can be a pretty annoying virus. For the Creation of the Virus follow these steps: Step 1: Press the Window + R Button from the Keyboard. This will open the Run Dialog book will Ope
2 min read
TCS Interview Experience | Set 15 - New 2017 Pattern In TCS 2017 campus placement, first round has introduced new pattern. This year onward, the test shall consist of 4 sections, instead of the earlier two. The duration, however, shall remain the same, i.e. 90 minutes. The details are: 1. Verbal Test (10 Mins) : This shall be similar to previous year'
4 min read