2016-04-10 19 views
1

Bir deyimde break deyimini kullanmaya çalışıyorum. Kullanıcı tarafından verilen en büyük asal sayıyı userInput kullanıcı tarafından daha az bulunan bir işlev oluşturmaya çalışıyorum. İşte benim kodudur:Bu özel durumda break yan tümcesi nasıl kullanılır?

Ben büyük asal bulduğunuzda programı durdurmak için break ifadesini koymak bilmemiz gerekiyor
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 

. Onu nereye koyacağım ve neden? Dönüş işlevi çalışmıyor. Olanlar şöyle:

[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

En büyük asal sayı 1000'den daha az mı demek istiyorsunuz?)? – mwm314

cevap

0

deneyin n çok büyük olduğunda daha iyi performans için 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

En büyük asal çarpanı elde etmek için yukarıdan aşağı doğru yinelemeyi düşünürsünüz. Bunun gibi:

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

Şu ana kadar kullandığınız kod oldukça iyi görünüyor. Tanımladığınız işlev, verilen (tek) tamsayı bir asal sayı olup olmadığını bulmaktır. Şimdi yapman gereken şey sadece 1000'den başlamak ve bir asal olana kadar inmek.

Sanırım numaranın asal olup olmadığına bağlı olarak True veya False ürününü döndürmek daha temizdir. Ayrıca, birincil bulma fonksiyonunuzun bir argümanı olarak userInput'u kabul etmek için biraz daha temizdir. Yani, öylesine gibi olmak işlevinizi değişebilir:

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 

ardından, (Sorunuzun göre, mola kullanarak) max bulmak için bunu yapabilirsiniz: Kullanılacak

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

Çok teşekkürler @ mwm314.Bu çok yardımcı olacak. – nabu

İlgili konular