首页
/
每日頭條
/
生活
/
python素數怎麼判斷
python素數怎麼判斷
更新时间:2024-11-26 11:33:18

python素數怎麼判斷?問題:判斷101-200之間有多少個素數,并輸出所有素數,我來為大家講解一下關于python素數怎麼判斷?跟着小編一起來看一看吧!

python素數怎麼判斷(Python判斷素數個數并輸出方法)1

python素數怎麼判斷

問題:判斷101-200之間有多少個素數,并輸出所有素數。

程序分析:判斷素數的方法:用一個數分别去除2到sqrt(這個數),如果能被整除,則表明此數不是素數,反之是素數。

程序源代碼:

實例(Python 2.0 )

#!/usr/bin/python

# -*- coding: UTF-8 -*-

h = 0

leap = 1

from math import sqrt

from sys import stdout

for m in range(101,201):

k = int(sqrt(m 1))

for i in range(2,k 1):

if m % i == 0:

leap = 0

break

if leap == 1:

print '%-4d' % m

h = 1

if h % 10 == 0:

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
Copyright 2023-2024 - www.tftnews.com All Rights Reserved