2014-09-25 20 views
0

我試圖在3之後生成第一個「n」素數。問題是我不明白我的代碼出錯了,並希望有人能夠幫助我處理邏輯。Python素數編碼

a = 3 
b = 2 

# n = number of prime numbers to be printed. 
n = 1000 

for a in range(a, n): #This i the range of all numbers to be tested 
    if a % 2 == 0:  #Only odd numbers are prime(except 2) so eliminate the evens 
     print "." 
else: 
    for b in range(b, a): #This is to test the odd numbers 
     if (a % b == 0): #I am dividing a by all numbers smaller than it to test 
      print a, "is not prime" 
      break 

任何指導幫助我解決這個問題將不勝感激。我閱讀了關於這個問題的舊帖子,但想知道我自己的邏輯在哪裏有缺陷。

謝謝。

+2

如果這真的是你的代碼看起來的樣子,那麼'else'就會有錯誤的縮進。 – 2014-09-25 20:00:03

+0

你有壞的縮進!'else'必須低於'if' – Kasramvd 2014-09-25 20:02:16

+0

我會說不要在範圍內(a,n):',對下界和索引器使用相同的變量,儘管它確實做它應該做的事情。此外,你沒有找到第一個'n'的素數,而是找到'n'的素數。 – mixedmath 2014-09-25 20:03:08

回答

0

我認爲你在這裏有幾個問題。

當涉及到else時,您的縮進是錯誤的。

此外,嘗試更改您在循環中使用的變量名稱,以便不使用在腳本頂部定義的a和b,您應該看到不同之處。

它應該是:

a = 3 
b = 2 

n = 1000 

for ax in range(a, n): 
    if ax % 2 == 0: 
     print "." 
    else: 
     for bx in range(b, ax): 
      if (ax % bx == 0): 
       print ax, "is not prime" 
       break 

添加另一個否則破發後也應該幫助你在調試這樣的問題。

+0

非常感謝您的幫助。我會重寫代碼,直到我瞭解細微差別。 – zigzagdoom 2014-09-25 20:32:10

+0

這就是爲什麼你應該有你的原始代碼和這個的例子,並在每一步打印a和b的值。如果您認爲它有幫助,請提出答案。 – 2014-09-25 20:33:23

+0

再次感謝。會upvote,但需要更多的代表。 – zigzagdoom 2014-09-25 20:37:18

0

首先你應該檢查縮進。我想這是你的代碼:

for a in range(a, n): #This i the range of all numbers to be tested 
    if a % 2 == 0:  #Only odd numbers are prime(except 2) so eliminate the evens 
     print "." 
    else: 
     for b in range(b, a): #This is to test the odd numbers 
      if (a % b == 0): #I am dividing a by all numbers smaller than it to test 
       print a, "is not prime" 
       break 

這段代碼對我有意義。您還需要修復第一張照片。

if a % 2 == 0:  #Only odd numbers are prime(except 2) so eliminate the evens 
     print a, "is not prime" 

現在讓我們看看邏輯。如果a是偶數,那麼它不是素數,你不能以a = 2開始。那麼,如果它是奇怪的,檢查是否有任何數字可以分割它。

有更好的算法來做到這一點。檢查this

+0

非常感謝。對於算法。我會嘗試並實施它。 – zigzagdoom 2014-09-26 10:43:07

0

下面是一些代碼:

import math 
import itertools 

def isprime(m): 
    return not any((m % d == 0 for d in range(2, int(math.sqrt(m)+1)))) 

def genprimes(n): 
    i = 0 
    for m in itertools.count(2): # 2, 3, 4, ... 
     if i >= n: 
      return 
     if isprime(m): 
      yield m 
      i += 1 

運行

list(genprimes(10)) 

產生

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29] 
+0

非常感謝! – zigzagdoom 2014-09-28 20:19:33

0
n = 1000 
    primes = [] 
    nonprime = [] 

    def prime(nonprime, primes, n): 
     nonprime.append(0) 
     nonprime.append(1) 
     nonprime.append(2) 
     primes.append(3) 
     for number in range(3, n): 
      if (number % 2 == 0): 
       nonprime.append(number) 
      else: 
       counter = 3 
       while counter <= number: 
        if (counter != number) and (number % counter == 0): 
         nonprime.append(number) 
         break 
        elif (number % counter == 1) and (counter >= number/2): 
         primes.append(number) 
         break 
        else: 
         counter += 1 

     print "primes are : ", primes 
     print "nonprimes are : ", nonprime 

    if __name__ == "__main__": 
     prime(nonprime, primes, n) 

顯然不是最有效的。希望它可以幫助某種方式。