smenks13/khan-exercises — pull request #1 by zachfogg (2011-10-12 19:16:05+00:00)
Title: Faster isPrime function.

To run a test and benchmark of the new function vs. the old function, run this:
https://gist.github.com/36b79fdba54912b5775d

Basically it works because all the factors of n are equal to or less than sqrt(n). So, we can cache sqrt(n) and count up to it.

Also, by checking if n % 2 === 0, we remove the need to test for any further even numbers. That allows us to increment our loop counter by two, decreasing the clock cycles required to determine if n is prime.