主要是依据质数符合6k+-1的规律,而取除数范围是2至根号n
import math
def find_primery(number):
#判断1,2,3
if number in [2,3]:
return 1
elif number == 1:
return 0
#能被2或3整除
if number%2 == 0 or number%3 == 0:
return 0#合数
#质数判别6k+-1
elif (number+1)%6 == 0 or (number-1)%6==0:
pass
else:
return 0 #合数
squrt_n = int(math.sqrt(number))#计算上限
if int(squrt_n+1)/6 > 1:#确定步进上限
step_max = int((squrt_n+1)/6)
else :
step_max = 1
for loop in range(1,step_max):#1到step_max为在2至根号number之间的质数的步进范围
print(loop)
if number%(loop*6+1)==0 or number%(loop*6-1)==0:#除以此范围内的所有质数
return 0
else:
pass #合数
return 1 #他是质数
if __name__ == '__main__':
while 1:
str1 = input('input number:')
number = int(str1)
print('result:%d'%(find_primery(number)))