Printing prime numbers from 1 through 100

前端 未结 22 2452
无人共我
无人共我 2020-11-28 05:14

This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97.

But I don\'t think tha

22条回答
  •  -上瘾入骨i
    2020-11-28 05:54

    If j is equal to sqrt(i) it might also be a valid factor, not only if it's smaller.

    To iterate up to and including sqrt(i) in your inner loop, you could write:

    for (int j=2; j*j<=i; j++)
    

    (Compared to using sqrt(i) this has the advantage to not need conversion to floating point numbers.)

提交回复
热议问题