Prime number calculation fun

前端 未结 18 1816
深忆病人
深忆病人 2020-12-05 15:32

We\'re having a bit of fun here at work. It all started with one of the guys setting up a Hackintosh and we were wondering whether it was faster than a Windows Box of (nearl

18条回答
  •  不知归路
    2020-12-05 15:37

    You should be able to make the inner loop twice as fast by only evaluating the odd numbers. Not sure if this is valid Java, I'm used to C++, but I'm sure it can be adapted.

                if (current != 2 && current % 2 == 0)
                        prime = false;
                else {
                        for (int i = 3; i < top; i+=2) {
                                if (current % i == 0) {
                                        prime = false;
                                        break;
                                }
                        }
                }
    

提交回复
热议问题