1

This python code generates prime numbers using Sieve's technique. I am having trouble understanding the code in particular the yield from parts. How to trace such a code or make it simpler to understand. This code is from the video from Computerphile Youtube channel about laziness in python.

def nats(n):
    yield n
    yield from nats(n+1)


def sieve(s):
    n = next(s)
    yield n
    yield from sieve(i for i in s if i%n != 0)
    
    
p = sieve(nats(2))

next(p) # generates prime numbers 2, 3, 5, 7, 11, ........
Ibrahim Sherif
  • 518
  • 1
  • 4
  • 15
  • you could start by adding print statements..... – Mitch Wheat Aug 20 '21 at 01:41
  • 2
    Does this answer your question? [Python example from Computerphile about yield and yield from](https://stackoverflow.com/a/64781874/15187728) – bb1 Aug 20 '21 at 02:24
  • Am I wrong, or is `nats` equivalent to [`itertools.count`](https://docs.python.org/3/library/itertools.html#itertools.count)? – Mark Ransom Aug 20 '21 at 02:49

0 Answers0