1 ''' 2 编写一个生成器函数myprimes(start, end), 3 实现[start, end)范围内的所有素数计算2 3 5 7. ... 4 (2,100) 5 ''' 6 7 def foo(start,end): 8 n = [] 9 m = [] 10 for i in range(start,end): 11 m.append(i) 12 for j in range(2,int(i/2)+1): 13 if i % j == 0: 14 n.append(i) 15 break 16 su = list(set(m)-set(n)) 17 for i in su: 18 yield i 19 def func(start,end): 20 for i in foo(start,end): 21 print(i) 22 23 func(2,10)
版本二:
1 def myprime(x): 2 if x <= 1: 3 return False 4 for i in range(2, x): 5 if x % i == 0: 6 return False 7 return True 8 def myprimes(start, end): 9 for i in range(start,end): 10 if myprime(i): 11 yield i 12 print([x for x in myprimes(1, 10)])