久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Python 3.5 中找到給定范圍內(nèi)的素?cái)?shù)總和?

How do I find the sum of prime numbers in a given range in Python 3.5?(如何在 Python 3.5 中找到給定范圍內(nèi)的素?cái)?shù)總和?)
本文介紹了如何在 Python 3.5 中找到給定范圍內(nèi)的素?cái)?shù)總和?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我設(shè)法使用以下方法創(chuàng)建了給定范圍內(nèi)的素?cái)?shù)列表:

I managed to create a list of prime numbers in a given range using this:

import numpy as np  

num = int(input("Enter a number: "))  

for a in range(2,num+1):         
  maxInt=int(np.sqrt(a)) + 1  
  for i in range(2,maxInt):
    if (a%i==0):  
      break  
  else: 
    print (a)

我現(xiàn)在想找到范圍內(nèi)所有素?cái)?shù)的總和,所以我就把它寫(xiě)下來(lái)

I want to now find the sum of all of the prime numbers in the range so I just put down

print (sum(a))

但在嘗試這樣做時(shí),我得到以下回溯:

But when trying to do that, I get the following traceback:

Traceback (most recent call last):
  File "C:/Users/Jason/PycharmProjects/stackidiots/scipuy.py", line 11, in <module>
    print(sum(a))
TypeError: 'int' object is not iterable

推薦答案

在您的情況下,a 是循環(huán)中使用的整數(shù)變量,不是 可迭代的.

In your case, a is an integer variable being used in your loop, not an iterable.

import numpy as np

num = int(input("Enter a number: "))

primes = []

for a in range(2,num+1):

  maxInt= int(np.sqrt(a)) + 1

  for i in range(2,maxInt):

    if (a%i==0):
      break

  else:
    primes.append(a)

print(sum(primes))

因此,如果我們只是將它們附加到列表中而不是打印它們,當(dāng)獲取列表 primessum 時(shí),我們會(huì)得到以下輸出.

So if we just append them to a list as we go instead of printing them, we get the following output when taking the sum of the list primes.

Enter a number: 43
281

這篇關(guān)于如何在 Python 3.5 中找到給定范圍內(nèi)的素?cái)?shù)總和?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 一级二级三级在线观看 | av一区二区三区四区 | 久久伊人操 | 日韩网站免费观看 | 日韩资源 | 伊人99| 国产美女久久 | 国产99久久精品 | 亚洲一区二区三区免费在线观看 | 一区中文字幕 | 亚洲第一成人影院 | 超碰成人免费观看 | 日韩中文欧美 | 久久久久久久久久久久一区二区 | 在线国产中文字幕 | 国产一区成人 | 欧美理论片在线观看 | 亚洲欧美自拍偷拍视频 | 亚洲91视频 | 亚洲国产一区二区三区在线观看 | 久久久久免费精品国产小说色大师 | 毛片在线免费 | 欧美成人精品二区三区99精品 | 伊人二区 | 在线观看国产视频 | 久久香焦| www.av7788.com| 欧美一区二区免费 | 国产精品久久久久无码av | 久久免费高清 | 亚洲 中文 欧美 日韩 在线观看 | 成人精品在线视频 | 综合一区二区三区 | 在线久草| 91av在线视频观看 | 亚洲一区 中文字幕 | 久久精品 | 亚洲国产一区二区三区, | 精品国产精品国产偷麻豆 | 国产精品成人在线观看 | 亚洲高清一区二区三区 |