2013-05-10 75 views
3

我寫了一個函數isprime(n),如果數字是素數則返回True,否則返回false。 我可以將函數循環定義次數;但我不知道如何迭代,直到找到x個素數。我感覺好像對For和While循環有一個體面的理解,但是對於如何將布爾返回值集成到循環中感到困惑。這裏是我當前的代碼和錯誤:迭代直到函數返回True用戶定義的次數

錯誤結果:

input:100 
Traceback (most recent call last): 
File "euler7.py", line 25, in <module> 
primeList += 1 
TypeError: 'int' object is not iterable 

,代碼:

def isprime(n): 
    x = 2 
    while x < sqrt(n): 
     if n % x == 0: 
      return False 
     else: 
      x += 1 
    return True 

userinput = int(raw_input('input:')) 

primeList = [] 
primesFound = 0 

while primesFound != userinput: 
    i = 2 
    if isprime(i): 
     primeList.append(i) 
     primeList += 1 
     i += 1 
    else: 
     i += 1 

編輯(包括更新和運行代碼):

from math import sqrt 
def isprime(n): 
    x = 2 
    while x < (sqrt(n) + 1): 
     if n % x == 0: 
      return False 
     else: 
      x += 1 
    return True 

userinput = int(raw_input('input:')) 
primeList = [] 
primeList.append(2) 

i = 2 
while len(primeList) != userinput: 
    if isprime(i): 
     primeList.append(i) 
     i += 1 
    else: 
     i += 1 

print 'result:', primeList[-1] 

回答

1

正如其他人所指出的:

  • 你應該增加primesFound,不primeList
  • isprime()函數有一個錯誤 - 並返回True爲9。您需要sqrt(n) + 1

另外:

  • 需要初始化iwhile外循環;否則,你只需建立一個2的列表。
  • 不需要primesFound。只需檢查len(primeList)

我的忌諱:

  • 命令行程序應該只在特殊情況下采取的交互式用戶輸入。在可能的情況下,將參數作爲命令行參數或選項。例如:userinput = int(sys.argv[1])
2

此行:

primeList += 1 

應該是:

primesFound += 1 
2

不能添加和int爲Python list。你應該做primesFound += 1來達到你想要的效果。

另外,你的isprime功能是錯誤的。它將返回True爲9.您應該爲isprime函數的while循環執行while x < sqrt(n) + 1

所以,你應該有:

def isprime(n): 
    x=2 
    while x < sqrt(n) +1: 
     if n % x == 0: 
      return False 
     else: 
      x += 1 
    return True 
1

要獲得n數滿足某個條件的,可以使用itertools.islice()功能和發電機的表達:

from itertools import count, islice 

n = int(raw_input('number of primes:')) 
primes = list(islice((p for p in count(2) if isprime(p)), n)) 

其中(p for p in count(2) if isprime(p))是發電機表達式無限期生成素數(也可以寫成itertools.ifilter(isprime, count(2)))。

你可以使用Sieve of Eratosthenes algorithm,以獲得更有效的解決方案:

def primes_upto(limit): 
    """Yield prime numbers less than `limit`.""" 
    isprime = [True] * limit 
    for n in xrange(2, limit): 
     if isprime[n]: 
      yield n 
      for m in xrange(n*n, limit, n): # mark multiples of n as composites 
       isprime[m] = False 

print list(primes_upto(60)) 
# -> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59] 

Fastest way to list all primes below N in python

注:約有limit/(log(limit) - 1)質數小於limit

你也可以使用無限素數生成器,如gen_primes(),拿到第一n質數號碼:

primes = list(islice(gen_primes(), n)) 

How to implement an efficient infinite generator of prime numbers in Python?

0
def is_prime(n): 
    x=2 
    while x < sqrt(n) +1: 
     if n % x == 0: 
      return False 
      break 
     else: 
      x += 1 
    return True