菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 练习实例12

Python 100例 Python 100例

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

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

程序源代码:

实例(Python 2.0+)

#!/usr/bin/python# -*- coding: UTF-8 -*-h = 0leap = 1frommathimportsqrtfromsysimportstdoutforminrange(101,201): k = int(sqrt(m + 1))foriinrange(2,k + 1): ifm % i == 0: leap = 0breakifleap == 1: print('%-4d' % m)h += 1ifh % 10 == 0: print('')leap = 1print('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

Python 100例 Python 100例

AI 思考中...

10 篇笔记 写笔记

  1. #0

    至尊宝

    521***[email protected]

    110

    使用集合解法:

    #!/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))
    

    至尊宝

    521***[email protected]

    9年前 (2017年04月08日)
  2. #0

    Eric

    382***[email protected]

    9

    参考解法:

    #!/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
    

    Eric

    382***[email protected]

    9年前 (2017年04月25日)
  3. #0

    swordzjc

    hfu***[email protected]

    6

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

    swordzjc

    hfu***[email protected]

    9年前 (2017年05月02日)
  4. #0

    奈琉

    115***[email protected]

    18

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

    #!/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))
    

    奈琉

    115***[email protected]

    9年前 (2017年05月06日)
  5. #0

    大愚

    923***[email protected]

    3

    参考方法:

    #!/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()
    

    大愚

    923***[email protected]

    9年前 (2017年05月08日)
  6. #0

    Kunz

    sun***[email protected]

    0

    参考方法:

    #!/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

    Kunz

    sun***[email protected]

    9年前 (2017年05月09日)
  7. #0

    Think-dfrent

    iwa***[email protected]

    4

    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)

    Think-dfrent

    iwa***[email protected]

    9年前 (2017年06月16日)
  8. #0

    AnnieHe

    380***[email protected]

    2

    参考方法:

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

    AnnieHe

    380***[email protected]

    9年前 (2017年09月18日)
  9. #0

    周周

    zhi***[email protected]

    2

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

    周周

    zhi***[email protected]

    9年前 (2017年10月17日)
  10. #0

    Echo

    csz***[email protected]

    6

    我写了两种方法,一种是迭代器:

    #!/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))

    Echo

    csz***[email protected]

    9年前 (2017年12月05日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /