Certainly! Here's the implementation of the isSemiprime function along with the auxiliary function isPrime:

def isPrime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

def isSemiprime(n):
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0 and isPrime(i) and isPrime(n // i):
            return True
    return False

In this implementation, the isPrime function checks if a number n is prime. It returns False if n is less than 2. Otherwise, it iterates from 2 to the square root of n and checks if n is divisible by any number in that range. If a divisor is found, it means that n is not prime and False is returned. Otherwise, it is determined that n is prime and True is returned.

The isSemiprime function checks if a number n is semiprime. It iterates from 2 to the square root of n and checks if n is divisible by any number in that range. If a divisor i is found, it checks if both i and n // i are prime using the isPrime function. If both are prime, it means that n is semiprime and True is returned. If no semiprime condition is satisfied, False is returned.

Now, let's run the provided test cases:

assert isSemiprime(6) == True
assert isSemiprime(9) == True
assert isSemiprime(1) == False
assert isSemiprime(7) == False
assert isSemiprime(12) == False

If the assertions pass without any AssertionError being raised, it indicates that the implementation of the isSemiprime function is correct.

Python Function to Determine if a Number is Semiprime

原文地址: https://www.cveoy.top/t/topic/WnS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录