python2.7练习小例子(十一)

    11):题目:判断101-200之间有多少个素数,并输出所有素数。

    程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

    程序源代码:

 


#!/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:
            print ''
    leap = 1
print 'The total is %d' % h

    以上实例输出结果为:

 

101 
103 
107 
109 
113 
127 
131 
137 
139 
149 

151 
157 
163 
167 
173 
179 
181 
191 
193 
197 

199 
The total is 21

    使用集合解法:

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

l = []
for i in range(101,200):
    for j in range(2,i-1):
        if i%j ==0:
            break
    else:
        l.append(i)

print(l)

print("总数为:%d" % len(l))

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-#

from math import sqrt

count=0
pn=1
for i in range(101,201):
    k=int(sqrt(i))
    for j in range(2,k+1):
        if i%j==0:
            pn=0
            break
    if pn==1:
        count+=1
        print i
    pn=1
print "total number is %d"%count

    Python3 测试实例:

 

#!/usr/bin/python3

list1 = []
list2 = []
for x in range(2, 101):
    for i in range(2, x+1):
        sum = x * i
        if (sum < 200) & (sum > 101):
            list1.append(sum)
for m in range(101, 200):
    list2.append(m)
list3 = list(set(list2) ^ set(list1))
print(list1, '\n')
print(list3)
print("总数为:", len(list3))

    判断素数的方式我选用了排除法,采用切片复制原有列表,逐一排除非素数,则剩余的列表中元素皆为素数:

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import math
m=range(101,201)
p=m[:]
for i in range(101,201):
    for j in range(2,int(math.sqrt(i)+1)):
        if i % j == 0:
            p.remove(i)
            break
print(p)
print("101至200之间的素数一共有%d个"%len(p))
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import math

def sushu():
    result = []
    for i in range(101,201):
        flag = True
        for j in range(2,int(math.sqrt(i))+1):
            if i % j == 0:
                flag = False
                continue
        if flag == True:
            result.append(i)
    print result

sushu()

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from math import sqrt

l=[]
for x in range(101,201):
    l.append(x)
    for i in range(2,int(sqrt(x))+1):
        if x%i==0:
            l.pop()
            break

n=len(l)
print l

print '总数为:',n

    python3 测试实例:去除除2以外的偶数 提高效率:

 

#!/usr/bin/env python3

import math

def sushu(start,end):
    count=0
    for i in range(start,end+1):
        if(i%2==0 and i!=2):                #去除除2以外的偶数
            continue
        for j in range(2,int(math.sqrt(i))+1):
            if(i%j==0):
                break
        else:
            count=count+1
            print(i,end=" ")
    print("")
    print("count",count)
    return
            
#start=int(input("start:\n"))
#end=int(input("end:\n"))
#sushu(start,end)

sushu(101,200)

    

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

def a(n):
   L = []
   for i in range(2,n-1):
       L.append(n%i)
   if  0 not in L:
      return True
print filter(a,range(101,200))

    Python3 测试:检查 y 能否被 2 到 y**0.5 之间的整数整除,如果能则 break,如果不能,将该数加入列表并 break:

 

#!/usr/bin/python3


def prim(m, n):
    arr = []
    for x in range(m, n + 1):
        for y in range(2, int(x ** 0.5)):
            if (x / y) == int(x / y):
                break
            else:
                arr.append(x)
                break
    return arr

print(prim(101, 200))

    迭代器:

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def prime():
    n = 2
    while 1:
        for i in range(2, n+1):
            if n%i:
                continue
            else:
                if i==n :
                    yield n
                else:
                    break
        n+=1
L = []
for i in prime():
    if 101<=i<=200:
        L.append(i)
    if i>=200:
        break
print('一共有{}个素数,这些素数分别是:{}'.format(len(L),L))

    运行结果为:

 

一共有21个素数,这些素数分别是:[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]

    生成器一行搞定:

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

L = list(filter(lambda x: x not in set([i for i in range(101,201) for j in range(2,i) if not i%j]), range(101,201)))
print('一共有{}个素数,这些素数分别是:{}'.format(len(L),L))

    好啦,到这里本次分享就结束了。如果感觉不错的话,请多多点赞支持哦。。。

  原文链接:https://blog.csdn.net/luyaran/article/details/80047270

posted @ 2018-04-25 11:46  luyaran  阅读(420)  评论(0编辑  收藏  举报