function isPrime(num) { // deal with 0, 1, 2, 3 if (num <= 3) { if (num <= 1) { return false; } return true; } // deal with multiples of 2 and 3 if (num % 2 == 0 || num % 3 == 0) { return false; } // test all numbers not divisible by 2 or 3 for (var i = 5; i < Math.sqrt(num) + 1; i += 6) { if (num % i == 0 || num % (i + 2) == 0) { return false; } } return true; } $("#go").click(function () { var candidate = $("#candidate").val() * 1; if (isNaN(candidate) || candidate < 0 || candidate > 9007199254740992) { $("#answers").append("Cannot test " + candidate.toString() + "<br>"); return; } if (isPrime(candidate)) { $("#answers").append(candidate.toString() + " is prime<br>"); } else { $("#answers").append(candidate.toString() + " is not prime<br>"); } });