To find first N prime numbers in python

前端 未结 29 2353
醉梦人生
醉梦人生 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 08:02

    def isprime(n):
        if n <= 1:
            return False
        for x in range(2, n):
            if n % x == 0:
                return False
        else:
            return True
    
    def list_prime(z):
        y = 0
        def to_infinity():
            index=0
            while 1:
                yield index
                index += 1
        for n in to_infinity():
            if y < z:
                if isprime(n):
                    y = y + 1
                    print(n, end='\n', flush=True)
            else:break
        print(f'\n {z} prime numbers are as above.')
    
    # put your range below
    list_prime(10)
    

提交回复
热议问题