A basic introduction to searching for prime numbers in Python and looking at a couple of tricks to improve the search space of your code.
Could be useful for A level students as this might be the sort of thing asked in a timed programming task!
Find my code here: https://www.pythonmorsels.com/p/2vg6r/
import time, math
number = int(input("Enter number to check if it's prime"))
start_time = time.time()
prime = True
for i in range(3,int(math.sqrt(number))+1,2):
if number % i == 0:
prime = False
break
end_time = time.time()
if prime == True:
print(number ,"is a prime number")
else:
print(number ,"is NOT a prime number")
print("Seconds:"+str(end_time - start_time))
Download
0 formats
No download links available.
Prime Numbers in Python - tricks to make your program find them faster | NatokHD