Jan-28-2022, 10:35 PM
(This post was last modified: Jan-28-2022, 10:35 PM by Gribouillis.)
#!/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)