斐波那锲数列
def fib(num):
a = 0
b = 1
n = 0
while n < num:
a, b = b , a + b
yield a
n += 1
print('done')
for i in fib(9):
print(i)
print(fib(9))
![](https://img2018.cnblogs.com/blog/1135753/201810/1135753-20181023212921222-1387293695.png)
杨辉三角
def triangles(num):
l = []
n = 0
c = 0
while n < num:
b = 0
result = []
if len(l):
for x in l[:]:
result.append(b + x)
b = x
l = result[:]
l.append(1)
yield l
n += 1
print('done')
for val in triangles(9):
print(val)
![](https://img2018.cnblogs.com/blog/1135753/201810/1135753-20181023212542404-1410178263.png)
素数
def nums():
n = 1
while True:
n += 1
yield n
def filterPrimes(n):
return lambda x: x % n > 0
def primes():
n = nums()
while True:
currentN = next(n)
yield currentN
n = filter(filterPrimes(currentN), n)
def showPrimeslist(total):
prime = primes()
result = []
index = 0
total = range(total)
for i in total:
result.append(next(prime))
while index < len(total):
print(result[index: index + 10])
index += 10
showPrimeslist(100)
![](https://img2018.cnblogs.com/blog/1135753/201810/1135753-20181024102133201-1269365444.png)