Day 25: Running Time and Complexity

  • + 11 comments

    A better solution is to store the number's square root in a variable before testing. Here is a C++ example:

    int sq = sqrt(n);
    for(int i = 2; i <= sq; i++) {
        if(n%i == 0) //NOT PRIME
    }
    

    This takes one operation out of the loop, saving a lot of CPU cycles when dealing with bigger numbers. This approach reduces the final test case by several seconds on my machine, and those numbers are only around 10^9.