Python: encuentra una secuencia de números primos

Comparte el tutorial de inteligencia artificial de una gran vaca. ¡Basado en cero! ¡Fácil de entender! ¡Divertido y chistoso! ¡Espero que también te unas al equipo de inteligencia artificial! Haga clic en 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()

 

Supongo que te gusta

Origin blog.csdn.net/chimomo/article/details/110437341
Recomendado
Clasificación