python prime number generator

Solutions on MaxInterview for python prime number generator by the best coders in the world

showing results for - "python prime number generator"
Lisandro
05 Aug 2019
1# Prime number generator
2def prime_generator(end):
3    for n in range(2, end):     # n starts from 2 to end
4        for x in range(2, n):   # check if x can be divided by n
5            if n % x == 0:      # if true then n is not prime
6                break
7        else:                   # if x is found after exhausting all values of x
8            yield n             # generate the prime
9
10
11g = prime_generator(1000)       # give firt 1000 prime numbers
12print(list(g))