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?
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.
- Primality Test | Set 1 (Introduction and School Method)
- Primality Test | Set 2 (Fermat Method)
- Primality Test | Set 3 (Miller–Rabin)
In below code optimized school method is discussed.
Program on Composite Numbers
- Find a range of composite numbers of given length
- Generate a list of n consecutive composite numbers (An interesting method)
- Sum and product of k smallest and k largest composite numbers in the array
- Product of all the Composite Numbers in an array
- Count and Sum of composite elements in an array
- Split n into maximum composite numbers
This article is contributed by Ajay Puri. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.