Simple prime number generator in Python

前端 未结 26 2217
感情败类
感情败类 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 07:53

    You can create a list of primes using list comprehensions in a fairly elegant manner. Taken from here:

    >>> noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
    >>> primes = [x for x in range(2, 50) if x not in noprimes]
    >>> print primes
    >>> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
    

提交回复
热议问题