我正在尝试在循环中使用break语句。我正在尝试构建一个函数,该函数用户userInput
找到小于给定数字的最大素数。这是我的代码:
def primeFinder():
aVar = 2 # This is a possible factor
divisorList = [] # This is a factor list of userInout, the inputted number
while aVar <= userInput: # Checking until the variable cannot be a factor
if userInput % aVar == 0: # If userInput divided by the variable has a remainder of zero, it means it is a factor
divisorList.append(aVar) # Adding the factor to th factor list
aVar+=1 # Making the possible factor numeral one bigger to test again
else:
aVar +=1 # Making the possible factor numeral one bigger to test again
print divisorList
if len(divisorList) > 1: # If there are more than one factor...
print "Your input",userInput, " is not prime" # It is not prime
else:
print "Your input", userInput," is prime." # It is prime
当我找到最大的素数时,我需要知道在哪里放置break语句来停止程序。我把它放在哪里,为什么? 返回功能不起作用。这是发生的事情:
[2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500, 1000]
Your input 1000 is not prime
[3, 9, 27, 37, 111, 333, 999]
Your input 999 is not prime
[2, 499, 998]
Your input 998 is not prime
[997]
Your input 997 is prime.
[2, 3, 4, 6, 12, 83, 166, 249, 332, 498, 996]
Your input 996 is not prime
[5, 199, 995]
Your input 995 is not prime
[2, 7, 14, 71, 142, 497, 994]
Your input 994 is not prime
[3, 331, 993]
Your input 993 is not prime
[2, 4, 8, 16, 31, 32, 62, 124, 248, 496, 992]
Your input 992 is not prime
[991]
Your input 991 is prime.
[2, 3, 5, 6, 9, 10, 11, 15, 18, 22, 30, 33, 45, 55, 66, 90, 99, 110, 165, 198, 330, 495, 990]
Your input 990 is not prime
答案 0 :(得分:1)
我认为你最好从上到下迭代以获得最大的素数因子。像这样:
primeFactor=1
for aVar in range(userInput-1,1,-1): #loop from userInput-1 to 2
if userInput % aVar == 0:
primeFactor=aVar
break;
print "The largest prime factor is "+str(primeFactor).
答案 1 :(得分:1)
到目前为止你的代码看起来很不错。你所描述的功能是找到一个给定的(单个)整数是素数。你现在要做的就是从1000开始直到找到素数为止。
根据数字是否为素数,我认为返回True
或False
更清晰。接受userInput
作为你的主要发现功能的论据也是一个小清洁。所以,你可以改变你的功能:
def primeFinder(userInput):
aVar = 2 # This is a possible factor
divisorList = [] # This is a factor list of userInout, the inputted number
while aVar <= userInput: # Checking until the variable cannot be a factor
if userInput % aVar == 0: # If userInput divided by the variable has a remainder of zero, it means it is a factor
divisorList.append(aVar) # Adding the factor to th factor list
aVar+=1 # Making the possible factor numeral one bigger to test again
else:
aVar +=1 # Making the possible factor numeral one bigger to test again
#print divisorList
if len(divisorList) > 1: # If there are more than one factor...
return False
else:
return True
然后,要找到最大值(根据您的问题使用休息),您可以这样做:
for num in range(1000, -1, -1): # count down from 1000
if primeFinder(num):
print "The largest prime under 1000 is:"
print num
break # Break out of loop after we've found largest result, as per question
答案 2 :(得分:0)
当n太大时,尝试使用xrange以获得更好的性能:
from math import sqrt
def is_prime(n):
if n in (2, 3):
return True
for i in xrange(2, int(sqrt(n)) + 1):
if n % i == 0:
return False
return True
def find_largest_prime(n):
for i in xrange(n - 1, 1, -1):
if is_prime(i):
return i
print(find_largest_prime(1000))