2016-04-10 62 views
1

我想在循環中使用break語句。我正在嘗試構建一個函數,用於查找用戶輸入的最小素數小於給定數字,userInput。這是我的代碼:如何在這個特定情況下使用break子句

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 

我需要知道在發現最大素數時停止程序的位置。我在哪裏放,爲什麼? 返回功能不起作用。這裏發生了什麼:

[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

你的意思是最大的質數小於1000;)? – mwm314

回答

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)) 
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

你到目前爲止的代碼看起來不錯。你所描述的功能是發現一個給定的(單個)整數是否爲素數。你現在要做的只是從1000開始,然後往下走,直到你找到一個素數。

我認爲根據數字是否爲素數,僅返回TrueFalse會更清晰。接受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 

,然後,找到最大(用break,按你的問題),你可以這樣做:

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 
+0

非常感謝@ mwm314.這將會非常有幫助。 – nabu