Python-Find a sequence of prime numbers

Share a big cow's artificial intelligence tutorial. Zero-based! Easy to understand! Funny and humorous! Hope you join the artificial intelligence team too! Please click 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()

 

Guess you like

Origin blog.csdn.net/chimomo/article/details/110437341