Sunday, 9 May 2021

Finding the prime number in the fastest way possible

Here is the code to find the prime number in python. 

#code:

 import time #this imports the timer.

num= int(input('key in a number'))
current= time.time() #this starts the timer
if(num >1):
for x in range(2,int(num/2)): #deviding the number to make the processing time shorter.
#see the value after the division
if (num%x == 0): #x is any number from the range (2 upto int(num/2)),
print (num/2)
print (num, "not prime")
break

else:
print (num, "prime")
else:
print (num, "not prime")
total=time.time()-current #this returns the time used.
if(num >1):
print (total*100000)

No comments:

Post a Comment