分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Print prime numbers within 1000
def main():
for n in primes():
if n < 1000:
print(n, end=' ')
else:
break
# Odd sequences
def _odd_iter():
n = 1
while True:
n = n + 2
yield n
# A filter function: not divisible
def _not_divisible(n):
return lambda x: x % n > 0
# Prime generator
def primes():
yield 2
# Initial sequence
it = _odd_iter()
while True:
# Get the first number of the sequence
n = next(it)
yield n
# Construct new sequence
it = filter(_not_divisible(n), it)
if __name__ == '__main__':
main()