Check whether a number is semiprime or not
Given a positive integer n. Find whether a number is a semiprime or not. Print True if number is semiprime else False. A semiprime is a natural number that is a product of two prime numbers.
Examples :
Input: 6 Output: True Explanation 6 is a semiprime number as it is a product of two prime numbers 2 and 3. Input: 9 Output: True Input: 8 Output: False
The approach is simple, factorize the given number by dividing it with the divisor of a number to remove the composite number. Meanwhile keep updating the count variable of prime number.
C
// C Program to check whether // number is semiprime or not #include <stdio.h> // Utility function to check whether // number is semiprime or not int checkSemiprime(int num) { int cnt = 0; for (int i = 2; cnt < 2 && i * i <= num; ++i) while (num % i == 0) num /= i, ++cnt; // Increment count // of prime numbers // If number is greater than 1, add it to // the count variable as it indicates the // number remain is prime number if (num > 1) ++cnt; // Return '1' if count is equal to '2' else // return '0' return cnt == 2; } // Function to print 'True' or 'False' // according to condition of semiprime void semiprime(int n) { if (checkSemiprime(n)) printf("True\n"); else printf("False\n"); } // Driver code int main() { int n = 6; semiprime(n); n = 8; semiprime(n); return 0; } |
Java
// Java Program to check whether // number is semiprime or not class GFG{ // Utility function to check whether // number is semiprime or not static int checkSemiprime(int num) { int cnt = 0; for (int i = 2; cnt < 2 && i * i <= num; ++i) while (num % i == 0){ num /= i; // Increment count // of prime numbers ++cnt; } // If number is greater than 1, // add it to the count variable // as it indicates the number // remain is prime number if (num > 1) ++cnt; // Return '1' if count is equal // to '2' else return '0' return cnt == 2 ? 1 : 0; } // Function to print 'True' or 'False' // according to condition of semiprime static void semiprime(int n) { if (checkSemiprime(n) != 0) System.out.printf("True\n"); else System.out.printf("False\n"); } // Driver code public static void main(String[] args) { int n = 6; semiprime(n); n = 8; semiprime(n); } } // This code is contributed by // Smitha Dinesh Semwal |
Python3
# Python Program to check whether # number is semiprime or not import math # Utility function to check whether # number is semiprime or not def checkSemiprime(num): cnt = 0 for i in range(2, int(math.sqrt(num)) + 1): while num % i == 0: num /= i cnt += 1 # Increment count # of prime number # If count is greater than 2, # break loop if cnt >= 2: break # If number is greater than 1, add it to # the count variable as it indicates the # number remain is prime number if(num > 1): cnt += 1 # Return '1' if count is equal to '2' else # return '0' return cnt == 2 # Function to print 'True' or 'False' # according to condition of semiprime def semiprime(n): if checkSemiprime(n) == True: print("True") else: print("False") # Driver code n = 6semiprime(n) n = 8semiprime(n); |
C#
// C# Program to check whether // number is semiprime or not using System; class GFG{ // Utility function to check whether // number is semiprime or not static int checkSemiprime(int num) { int cnt = 0; for (int i = 2; cnt < 2 && i * i <= num; ++i) while (num % i == 0){ num /= i; // Increment count // of prime numbers ++cnt; } // If number is greater than 1, // add it to the count variable // as it indicates the number // remain is prime number if (num > 1) ++cnt; // Return '1' if count is equal // to '2' else return '0' return cnt == 2 ? 1 : 0; } // Function to print 'True' or 'False' // according to condition of semiprime static void semiprime(int n) { if (checkSemiprime(n) != 0) Console.WriteLine("True"); else Console.WriteLine("False"); } // Driver code public static void Main() { int n = 6; semiprime(n); n = 8; semiprime(n); } } // This code is contributed by vt_m. |
PHP
<?php // PHP Program to check whether // number is semiprime or not // Utility function to check whether // number is semiprime or not function checkSemiprime($num) { $cnt = 0; for ( $i = 2; $cnt < 2 && $i * $i <= $num; ++$i) while ($num % $i == 0) $num /= $i; // Increment count of // prime numbers ++$cnt; // If number is greater than 1, // add it to the count variable // as it indicates the number // remain is prime number if ($num > 1) ++$cnt; // Return '1' if count is // equal to '2' // else return '0' return $cnt == 2; } // Function to print 'True' or 'False' // according to condition of semiprime function semiprime($n) { if (checkSemiprime($n)) echo "True\n"; else echo "False\n"; } // Driver code $n = 6; semiprime($n); $n = 8; semiprime($n); // This code is contributed by anuj_67. ?> |
Output :
True False
Time complexity: O(
)
Auxiliary space: O(1)
Reference: https://en.wikipedia.org/wiki/Semiprime
Recommended Posts:
- Check if a number is divisible by all prime divisors of another number
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Check if a number with even number of digits is palindrome or not
- Program to check whether a number is Proth number or not
- Check if a number is a Mystery Number
- Check if a number is an Achilles number or not
- Check if a number is Flavius Number
- Check if given number is Emirp Number or not
- Check whether the given number is Euclid Number or not
- Check if a given number is factorial of any number
- Check whether a number is Non-hypotenuse number
- Check if a number is Triperfect Number
- Check if a number is a power of another number
- How to check if a given number is Fibonacci number?
- Check if a number is an Unusual Number or not
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 : vt_m



