这个素数输出出了什么问题?

时间:2013-10-15 03:24:24

标签: python

from math import sqrt

for num in range(2,100) :
    flag = True
    for sub in range(2,int(sqrt(num))+1) :
        if num % sub ==0 :
            flag = False
            break
        print num ,

This is the output, repeated numbers

我是python中的“品牌”新手,我尝试将代码从2输出到100,然后我 遇到一些问题,请帮帮我^^

1 个答案:

答案 0 :(得分:4)

 for sub in range(2,int(sqrt(num))+1) :
    if num % sub ==0 :
        flag = False
        break
    print num , # <<-- this is inside the for loop and will be printed every iteration