Simple prime number generator in Python

前端 未结 26 1780
感情败类
感情败类 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:20
    print [x for x in range(2,100) if not [t for t in range(2,x) if not x%t]]
    
    0 讨论(0)
  • 2020-11-22 08:20

    You need to make sure that all possible divisors don't evenly divide the number you're checking. In this case you'll print the number you're checking any time just one of the possible divisors doesn't evenly divide the number.

    Also you don't want to use a continue statement because a continue will just cause it to check the next possible divisor when you've already found out that the number is not a prime.

    0 讨论(0)
提交回复
热议问题