Python:每日一题 8
题目:判断101-200之间有多少个素数,并输出所有素数。程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
欢迎小伙伴们,一起答题!
如果你有能力,欢迎加入我们!
已经上车老司机:@ooxx7788 @lumber2388779
{:10_298:} 点我上车{:10_298:} @lumber2388779 @ooxx7788 @jerryxjr1220 @冬雪雪冬
来吧!新题目来了! 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:
print('')
leap = 1
print('The total is %d' % h)
等待被大佬们指点!!! 本帖最后由 ooxx7788 于 2017-3-27 21:15 编辑
def prime(s, e):
flag = 1
count = 0
for i in range(s, e, 2):
for j in range(2, int(i ** 0.5)+1):
if i % j == 0:
flag = 0
break
if flag:
print('{:>{}}'.format(i, len(str(e))), end=' ')
count += 1
if count == 10:
print()
count = 0
else:
flag = 1
if __name__ == '__main__':
prime(101, 200)
print()
prime(1, 2000)
输出
101 103 107 109 113 127 131 137 139 149
151 157 163 167 173 179 181 191 193 197
199
list1=[]
list2=[]
count=0
for i in range(101,201):
for j in range(2,int(i**0.5)+1):
if i%j==0:
list1.append(i)
for m in range(101,201):
list2.append(m)
for a in list2:
if a not in list1:
count+=1
print(a,end="")
print('总计%d个素数' % count)
101103107109113127131137139149151157163167173179181191193197199总计21个素数 xunzhao 发表于 2017-3-27 21:53
谢谢支持
点我上车 ooxx7788 发表于 2017-3-27 21:10
输出
妥妥的 新手·ing 发表于 2017-3-27 21:56
谢谢支持
点我上车
上车???怎么弄 primenum = []
notprimenum = []
for i in range(101,201):
maxnum = int(i**0.5)
minnum = 2
flag = True
while True:
if i%maxnum == 0:
flag = False
break
elif i%minnum == 0:
flag = False
break
elif minnum == maxnum or (minnum+1) == maxnum:
flag = True
break
else:
maxnum -= 1
minnum += 1
if flag:
primenum.append(i)
else:
notprimenum.append(i)
print('范围101-200中素数有:',primenum)
print('非素数有:',notprimenum)
结果输出
范围101-200中素数有:
非素数有: xunzhao 发表于 2017-3-27 22:39
上车???怎么弄
点击链接,可以订阅,也可以一起出题 lumber2388779 发表于 2017-3-28 15:24
结果输出
支持! 这个偷个懒,不写了 只能想到循环求余,但是数字来到 7位数就跑不动了, 应该是嵌套循环的锅
def find_prime(a,b):
"""输入两参数,寻找范围内的质数,目前支持从3开始往后的整数,a>b就不鸟你了"""
nums = []
for i in range(a,b+1):
sqrt_i = int(i**0.5) + 1
for j in range(2,sqrt_i):
if i%j==0:
break
elif j == sqrt_i - 1:
nums.append(i)
else:
continue
print(nums)
print("共有 %d 个" % len(nums))
find_prime(101,200)
#结果
共有 21 个
找到一行的代码,每一个要判断的数都形成求余的列表,用成员关系判断,真厉害
不过没有break的机制,循环的次数估计要多很多
print(]) a=0
for i in range(101,201):
for j in range(1,i+1):
if i%j==0:
a+=1
if a==2:
print(i)
a=0 import math
count = 0 #记素数的个数
n = 0 #记数排版
for i in range(101,200):
s = int(math.sqrt(i))
isPrime = True
for j in range(2,s+1):
if i%j == 0:
isPrime = False
break
if isPrime == True:
print(i,'\t',end = '')
n += 1
count += 1
if n == 5:
n = 0
print()
print('\nTotal:',count) i = 101
while i<=200:
n = 2
r = 0
while n<i**0.5:
if i%n == 0:
r = 1
break
n += 1
if r==0:
print (i)
i += 1
本帖最后由 抑痒指 于 2017-6-10 15:19 编辑
import math
def fuction1(a):
'''判断a是否是素数,若是,返回1,否则返回0'''
i = int(math.sqrt(a))
j = 1
for z in range(1,i+1):
for x in range(i,a):
if z*x ==a:
return 0
return 1
'''
测试函数1代码
print(fuction1(101))
'''
j=0
for i in range(101,201):
if fuction1(i):
j += 1
print(j)
def prime_number(x,y):
result = []
for i in range(x,y+1):
if i == 2:
result.append(i)
continue
for j in range(2,i):
if i%j == 0:
break
else:
result.append(i)
return result
if __name__ == '__main__':
print(prime_number(101,200)) import math
def getPrime():
prime_list = []
count = 0
for i in range(101,201):
flag = 0
for j in range(2,int(math.sqrt(i)+1)):
if i % j == 0:
flag = 1
if flag == 0:
count += 1
prime_list.append(i)
return prime_list,count
p,c = getPrime()
print('101-200之间总共有%d个素数' % c)
print(*p) 本帖最后由 木一 于 2017-8-20 21:08 编辑
import math as m
n = 0
list1 = []
for i in range(101,201):
for a in range(2,int(m.sqrt(i)) + 1):
if i % a != 0:
s = 0
else:
s = -1
break
if s == 0:
n += 1
list1.append(i)
print('共有%d个素数'%n)
print('它们分别是:')
for each in list1:
print(each,end = ' ')
{:10_243:}