wdjh.net
当前位置:首页 >> Python中用gEnErAtor生成素数表的问题...求助... >>

Python中用gEnErAtor生成素数表的问题...求助...

首先你要想一想在 python里面 tuple有什么样的特性? 然后再参考下面的代码def sieve(): g=[x for x in xrange(2,1000000)] while True: n=next(iter(g)) yield n g=[x for x in g if x%n!=0]

def getprimelist(n): if n

from math import sqrtdef isprime(x): if x == 1: return False k = int(sqrt(x)) for j in range(2, k+1): if x%j == 0: return False return Trueif __name__ == "__main__": flag = 'y' while(flag == 'y'): num = input("Please input a nu...

def primes(x): # prepair data space plist = [0, 0] + range(2,x+1) for i in xrange(2, x): if plist[i]: plist[i+i::i] = [0] * len(plist[i+i::i]) return filter(None, plist)print primes(100)筛选法是高效的素数列表计算算法, python的...

这是3-100的素数,供参考! def is_prime(n): i = 2 while(i < n): if n % i == 0: break i += 1 if n == i: return True else: return False p = 0 for i in range(3,101): if is_prime(i): print i, p += 1 if p % 10 == 0: print '\n'

n = 10000numbers = list(range(2, n + 1))i = 2while i < 10001: for m in numbers: if i < m: while m % i == 0:# 这边你为什么要用while呢,这边如果m=3,i=2的话是会进入死循环的,改成if语句试试 k = m #这个没必要存在,只接用numbers.remo...

num = int(input("please enter the number:")) for i in range(2, num): if num % i == 0: print(" %d is not a prime number!" % num) break else: print(" %d is a prime number!" % num)

def prime(n): if n

a = 0 list = [] for i in range(101,200,2): flag=0 for j in range(2,i/2+1): if (i%j == 0): flag = 0 break else: flag = 1 continue if flag == 1: list.append(i) a+=1 print list print a

第一空填k+1 for j in range(2,k+1) 第二空填i for i in range(2,num);

网站首页 | 网站地图
All rights reserved Powered by www.wdjh.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com