Optimizing prime numbers code?

后端 未结 9 1956
别那么骄傲
别那么骄傲 2021-01-25 01:53

I wrote this code to show the primes between 1 and 100. The only condition is to do not use functions, whole code should be inline. I would ask if I can improve (optimize) it mu

9条回答
  •  南笙
    南笙 (楼主)
    2021-01-25 02:24

        for(int j=2;j

    This one is not nice.

    First of all, you only need to check for j <= sqrt(i), as for example 7 will never divide 12 without a rest.

    Second, you should keep track of all previously found prime numbers; keep it in vector and only do this loop for it's contents and for that condition I wrote.

提交回复
热议问题