(In Python) Complete the isPrime() function to take an int and return True if it is a prime number and False if it is not.
Note: A prime number is not evenly divisible (meaning, the remainder is not 0) for all numbers smaller than itself (down to 2). For example, if num is 7, then you could test:
if 7 remainder 6 is not 0, then
if 7 remainder 5 is not 0, then
if 7 remainder 4 is not 0, then
if 7 remainder 3 is not 0 and finally
if 7 remainder 2 is not 0.
If it fails any one of these tests, then num is not prime. Otherwise, it is prime.
Examples:
isPrime( 2 ) returns True
isPrime( 3 ) returns True
isPrime( 4 ) returns False (4 is divisible by 2)
isPrime( 7 ) returns True
isPrime( 9 ) returns False (9 is evenly divisible by 3)
isPrime( 101 ) returns True
Source Code:
def isPrime(num):
for i in range(2,num-1):
if(num%i==0):
return False
return True
print(isPrime(2))
print(isPrime(3))
print(isPrime(4))
print(isPrime(7))
print(isPrime(9))
print(isPrime(101))
Get Answers For Free
Most questions answered within 1 hours.