给定正整数n。查找数字是否为半素数。如果number是半素数,则输出True ,否则返回False 。半素数是一个自然数,它是两个素数的乘积。
例子 :
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
该方法很简单,将给定数字除以数字的除数就可以分解出复合数字。同时不断更新素数计数变量。
C++
// C++ Program to check whether
// number is semiprime or not
#include
using namespace std;
// 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))
cout << "True\n";
else
cout << "False\n";
}
// Driver code
int main()
{
int n = 6;
semiprime(n);
n = 8;
semiprime(n);
return 0;
}
// This code is contributed by rutvik_56.
C
// C Program to check whether
// number is semiprime or not
#include
// 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 = 6
semiprime(n)
n = 8
semiprime(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
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.
?>
Javascript
输出 :
True
False
时间复杂度: O( )
辅助空间: O(1)
参考: https : //en.wikipedia.org/wiki/Semiprime