小山90
发表于 2017-8-23 10:55:29
print("----------判断101-200之间有多少个素数----------")
count = 0
for i in range(101,200):
flag = 0
for j in range(2,int(i**0.5)+1):
if i % j == 0: #如果不是素数
flag = 1
if flag == 0:
count += 1
print(i)
print("101-200之间有 %d 个素数" %count)
有一个问题想请教一下,我想在第一次判断出不是素数后就不再for循环了,这个有什么办法?我在flag=1后加break有效吗?
gausser
发表于 2017-9-8 23:17:11
import math
# True: the number is prime; False: the number is not prime
def JudgePrimeNumber(number):
sqrt_number = int(math.sqrt(number))
for i in range(2, sqrt_number + 1):
if number % i == 0:
return False
return True
print "Prime number is: "
for i in range(101, 201):
if JudgePrimeNumber(i) == True:
print i,
张大象
发表于 2017-9-14 15:44:16
count = 0
for i in range(101,200):
tmp = 0
for j in range(2,i):
if i%j == 0:
tmp = 1
break
if tmp == 0:
print(i,end = ' ')
count += 1
if count == 10:
print()
count = 0
pillar
发表于 2017-9-14 20:47:40
temp=[]
count=0
for i in range(101,201):
flag=0
for j in range(2,i):
if i%j==0:
# 判断不是素数
flag=1
break
if flag==0:
count+=1
temp.append(i)
print('101-200之间有%d个素数'%count)
print(temp)
weili2017
发表于 2017-9-15 00:20:53
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#判断101-200之间有多少个素数,并输出所有素数。
#判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数
#1.先判断101是不是素数
import math
a = 101
sushunum = 0
while a <= 200:
x = int(math.sqrt(a))
n = 2
while n <= x:
if a % n == 0:
break
else:
n = n+1
if n == x + 1:
print a, "是素数"
sushunum += 1
a += 1
print "101-200之间的素数的个数为",sushunum
elbert
发表于 2017-9-23 16:31:21
import math
for x in range(101,200):
m = int(math.sqrt(x))
for i in range(2,m+1):
if x % i ==0:
break
else:
print(x)
想看看大佬们用一行写完{:10_277:}
concrete
发表于 2017-9-23 19:39:59
import math
for i in range(101,200):
for j in range(i//2,math.ceil(math.sqrt(i)),-1):
if i%j == 0:
break
else:
print(i)
aixuexi82
发表于 2017-9-27 15:11:11
result = set()
for i in range(101,201):
for j in range(2,i):
if i % j == 0:
result.add(i)
for i in range(101,201):
if i not in result:
print(i,end=' ')
print('\n一共有%s个素数。' % (100-len(result)))
易水寒楠
发表于 2017-10-5 22:24:34
学习使用for 语句中break和continue用法
# -*-coding:gbk-*-
__author__ = 'chennan'
import math as m
def issushu(x,y):
list1=[]
for s in range(x,y+1):
for i in range(2,int(m.sqrt(s))+1):
if s%i == 0:
break
if i != int(m.sqrt(s)):
continue
else:
list1.append(s)
return list1
Ss=issushu(101,200)
for i in Ss:
print(i)
count=len(Ss)
print("总共%s个素数"%count)
天使骑魔鬼
发表于 2017-10-10 14:38:42
import math
a =
b = []
for i in range(101,201):
for j in range(2,int(math.sqrt(i)) + 1):
if i % j == 0:
b.append(i)
break
c = set(a) - set(b)
print(len(c),sorted(c))
石小龙
发表于 2017-11-9 17:34:49
from math import sqrt
def prime(n):
for i in range(2,int(sqrt(n)) + 1):
if n % i == 0:
return 'a'
return n
b = []
for n in range(101,201):
a = prime(n)
b.append(a)
while 'a' in b:
b.remove('a')
print(len(b),b)
Wesleyz
发表于 2017-11-11 16:25:58
这应该很简单啊,为什么需要那么多代码'''题目:判断101-200之间有多少个素数,并输出所有素数。'''
def isPrime(n):
try:
for i in range(2, n):
if n % i == 0:
return False
return True
except TypeError:
exit()
for i in range(100, 200):
if isPrime(i) == True:
print(i)
david_van
发表于 2017-11-27 23:27:41
本帖最后由 david_van 于 2017-11-27 23:31 编辑
import math
l = []#存放素数
for i in range(101,201):
#temp = math.sqrt(i)//1 + 1#此处写错了,应该为
temp = int(math.sqrt(i))+1#此处int()是向下取整数
for j in range(2,temp):
if i%j == 0:
break
if j == temp-1:
l.append(i)
print(l)
print('The total is %d' % len(l))
wojiaodabai
发表于 2018-1-17 16:08:51
def isPrime(low,up):
new=[]
for i in range(low,up+1):
fig=0
for j in range(2,i):
if i%j==0:
fig=1
break
if fig==0:
new.append(i)
print(new,len(new))
isPrime(101,200)
有鼻泡的犀牛
发表于 2018-1-17 17:11:32
def z(a):
for i in range(2,a // 2):
if( a % i ==0):
return False
if( i == a//2 - 1):
return True
for i in range(100,1000):
if(z(i)):
print(i)
1141429506
发表于 2018-1-27 21:04:44
'''题目:判断101-200之间有多少个素数,并输出所有素数。
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。'''
import math
list1=[]
list2=[]
for x in range(1,100):
for i in range(2,30):
for j in range(2,100):
if(i*j==x):
list1.append(x)
if(len(list1)==0):#i,j从2开始,如果除了1和x本身他还有别的因子,那么它就不再是素数,在这里
list2.append(x)
list1=[]
print(list2)
z1446773686
发表于 2018-2-11 13:44:05
import math
susu = []
for each in range(101,200):
blank = 1
for xeach in range(2,int(math.sqrt(each))):
if each % xeach == 0:
blank = 0
if blank == 1:
susu.append(each)
print('是素数的有:',susu)
运行结果是:
是素数的有:
虽然做出来了但仍然不理解为什么要除到sqrt()
大头目
发表于 2018-2-13 15:33:36
import math
list1 = []
for i in range(101,201):
n = 1
for j in range(2,int(math.sqrt(i))+1):
if i % j == 0:
n = 0
break
if n:
list1.append(i)
print(list1)
凌九霄
发表于 2018-2-16 08:55:27
import math
for i in range(101, 201):
b = 0
j = 2
for j in range(2, int(math.sqrt(i))):
a = i % j
if a == 0:
b += 1
if b == 0:
print(i)
小强森
发表于 2018-2-18 16:09:16
#【程序9】题目:判断101到200间有多少个素数,输入所有素数
a=1
for i in range(101,201):
for j in range(2,i):
if i%j==0:
break
if j+1==i:
a+=1
print(i)
print('共有%s个素数。'%a)