2017-06-06 18 views
-1

該函數應返回m和n之間的素數列表。如果m和/或n是素數,它們也應該包含在列表中。返回素數

這裏是我做了什麼:
Code

下面是應該是輸出:
請忽略前兩個輸出爲我做了他們。 Output file

+0

請不要發佈您的代碼的圖像。編輯你的問題,並粘貼問題中的實際代碼,並使用代碼格式工具來相應地構建它。另外,粘貼您的輸出也作爲文本。 – idjaw

+0

你的問題是什麼? –

+0

@idjaw因爲我不知道如何格式化我的代碼併發布它,這就是我爲什麼截圖的原因。 – hpotter054

回答

-1
def primes(m, n): 
    list_of_primes = []  # Keep track of primes 
    for num in range(m, n + 1): # make inclusive of upper bound 
     is_prime = True   # Assume number is prime as starting condition 
     for i in range(2, num): # Check remainder from number 2 up to num 
      if num % i == 0: 
       is_prime = False 
       break 
     if is_prime and num > 1:   #EDIT HERE 
      list_of_primes.append(num) #add to the list of primes if True 
    return list_of_primes 

注有多種方法來改善這個算法,如不檢查偶數ECT,但是這符合在你的問題中定義的標準。

+0

謝謝,但我怎麼不包括1它,而m = 1和n = 10 – hpotter054

+0

檢查編輯。現在只會在素數列表中添加數字,如果它們大於1。 – Rosh

+0

謝謝!這有助於 – hpotter054