To find first N prime numbers in python

前端 未结 29 2317
醉梦人生
醉梦人生 2020-11-28 06:56

I am new to the programming world. I was just writing this code in python to generate N prime numbers. User should input the value for N which is the total number of prime n

29条回答
  •  醉话见心
    2020-11-28 07:53

    Using generator expressions to create a sequence of all primes and slice the 100th out of that.

    from itertools import count, islice
    primes = (n for n in count(2) if all(n % d for d in range(2, n)))
    print("100th prime is %d" % next(islice(primes, 99, 100)))
    

提交回复
热议问题