Simple prime number generator in Python

前端 未结 26 2137
感情败类
感情败类 2020-11-22 07:16

Could someone please tell me what I\'m doing wrong with this code? It is just printing \'count\' anyway. I just want a very simple prime generator (nothing fancy).

26条回答
  •  余生分开走
    2020-11-22 08:04

    To my opinion it is always best to take the functional approach,

    So I create a function first to find out if the number is prime or not then use it in loop or other place as necessary.

    def isprime(n):
          for x in range(2,n):
            if n%x == 0:
                return False
        return True
    

    Then run a simple list comprehension or generator expression to get your list of prime,

    [x for x in range(1,100) if isprime(x)]
    

提交回复
热议问题