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

如何在 Python 3.5 中找到給定范圍內的素數總和?

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

問題描述

我設法使用以下方法創建了給定范圍內的素數列表:

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)

我現在想找到范圍內所有素數的總和,所以我就把它寫下來

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

print (sum(a))

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

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 是循環中使用的整數變量,不是 可迭代的.

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))

因此,如果我們只是將它們附加到列表中而不是打印它們,當獲取列表 primessum 時,我們會得到以下輸出.

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

這篇關于如何在 Python 3.5 中找到給定范圍內的素數總和?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美精品乱码视频一二专区 | 国产成人免费观看 | 伊人在线视频 | 青青操影院 | 亚洲免费精品 | 真实的国产乱xxxx在线 | 一级片在线观看视频 | 久久久成人免费视频 | 国产精品久久久久久久久久久久久 | 一区二区三区在线免费观看 | 国产精品一区在线播放 | 每日更新av| 欧美精品一 | 五月婷丁香 | 国产精品6 | 久久久www成人免费精品 | 99久久视频 | 三级在线观看视频 | 亚洲天堂av在线 | 88av视频 | 日韩国产中文字幕 | 久久xxxx| 中文字幕色哟哟 | 日韩在线视频观看 | 操bbbbb| 99久久久国产精品 | 午夜视频在线 | www.99色| 亚洲久草 | 色综合天天综合网国产成人网 | 高清乱码男女免费观看 | 色多多视频在线观看 | 免费黄色网址在线观看 | 国产午夜精品一区二区三区视频 | 在线视频一区二区 | 亚洲裸体视频 | 在线国产小视频 | 国产午夜影院 | www.粉色视频在线观看 | 国产视频999 | 日本视频在线播放 |