Question: (python) A more efficient isPrime(n) can be written than the one we wrote in class. To check that a positive int is prime, it is
(python) A more efficient isPrime(n) can be written than the one we wrote in class. To check that a positive int is prime, it is not necessary to check all potential factors f in the range 2 <= f < n. It is sufficient to check potential factors f in the range 2 <= f <= int(math.sqrt(n)) + 1. Write an improved boolean function, isPrime(n), which uses this new idea. Make sure your function works. Note that 1 is NOT a prime number. Sample run: large_prime = 2**31 - 1 ## this is a Mersenne prime. # See: https://primes.utm.edu/mersenne/index.html for n in [1,2,3,4,5,6,7,8,9,10,11,12,13,large_prime]: print(n,isPrime(n),end = ', ') 1 False, 2 True, 3 True, 4 False, 5 True, 6 False, 7 True, 8 False, 9 False, 10 False, 11 True, 12 False, 13 True, 2147483647 True,
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
