Python isSemiprime Function Explained: Checking for Semiprime Numbers
Apologies for the confusion. To clarify, 'n' is the parameter of the 'isSemiprime' function, representing the positive integer being checked for being a semiprime. Here's the complete code with the correct naming:
def isPrime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def isSemiprime(n):
prime_count = 0
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
if isPrime(i) and isPrime(n // i):
prime_count += 1
return prime_count == 2
Now, you can test the 'isSemiprime' function using the provided test cases:
assert isSemiprime(6) == True
assert isSemiprime(9) == True
assert isSemiprime(1) == False
assert isSemiprime(7) == False
assert isSemiprime(12) == False
In these assertions, 'n' is replaced with the respective positive integers being tested.
原文地址: https://www.cveoy.top/t/topic/SPi 著作权归作者所有。请勿转载和采集!