Simple prime number generator in Python

前端 未结 26 1756
感情败类
感情败类 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:57

    This seems homework-y, so I'll give a hint rather than a detailed explanation. Correct me if I've assumed wrong.

    You're doing fine as far as bailing out when you see an even divisor.

    But you're printing 'count' as soon as you see even one number that doesn't divide into it. 2, for instance, does not divide evenly into 9. But that doesn't make 9 a prime. You might want to keep going until you're sure no number in the range matches.

    (as others have replied, a Sieve is a much more efficient way to go... just trying to help you understand why this specific code isn't doing what you want)

提交回复
热议问题