1
2function isPrime(n)
3{
4 var divisor = 3,
5 limit = Math.sqrt(n);
6
7 //check simple cases
8 if (n == 2 || n == 3)
9 return true;
10 if (n % 2 == 0)
11 return false;
12
13 while (divisor <= limit)
14 {
15 if (n % divisor == 0)
16 return false;
17 else
18 divisor += 2;
19 }
20 return true;
21}
22
23> isPrime(137);
24 = true
25> isPrime(237);
26 = false
27