Jan-28-2022, 10:35 PM
(This post was last modified: Jan-28-2022, 10:35 PM by Gribouillis.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#!/usr/bin/env python # SPDX-FileCopyrightText: 2022 Member Gribouillis of www.python-forum.io # # SPDX-License-Identifier: MIT __version__ = '2022.01.28.2' from heapq import heappush, heappop, heappushpop import itertools as itt def primes(): """Return the infinite sequence of all prime numbers""" yield 2 h = [] n = 3 x, i, seq = 4 , 2 , itt.count( 6 , 2 ) while True : if n < x: yield n n2 = n * * 2 heappush(h, (n2, n, itt.count(n2 + n, n))) n = x + 1 x, i, seq = heappushpop(h, ( next (seq), i, seq)) if __name__ = = '__main__' : s = primes() # print the first prime numbers for i in range ( 100 ): p = next (s) print (p) |