我正在尝试编写一个程序来告诉您一个数字是否为素数。这里是。正如您所看到的,当您运行该chkPrime
函数时,一切都返回 true。我这辈子都想不通。有任何想法吗?
total=0
#Check if number is whole
def chkWhole(x):
if(x%1 == 0):
return True
else:
return False
#Check to see if the number divides evenly with all primes
def chkModtwo(n):
a=n%2
if chkWhole(a)==True:
return True
else:
return False
def chkModthree(n):
a=n%3
if chkWhole(a)==True:
return True
else:
return False
def chkModfive(n):
a=n%5
if chkWhole(a)==True:
return True
else:
return False
def chkModseven(n):
a=n%7
if chkWhole(a)==True:
return True
else:
return False
#Check if the number is a prime using other two functions
def chkPrime(n):
if n== 1 or 2 or 3 or 5 or 7:
return True
if chkModtwo(n)==False and chkModthree(n)==False and chkModfive(n)==False and chkModseven(n)==False:
return True
else:
return False
#while True:
#yourinput=raw_input("Enter to check if it is a prime")
#
#
# youranswer=chkPrime(yourinput)
#
# if youranswer==True:
# print("Yes, it is a prime")
# else:
# print("No, this number is not prime")