### Prime Numbers

Below is a javascript function that returns, in an array, all prime numbers between two integers. There is little validation - the function will not work if (a||b)<=1.

This function says for all numbers between the two passed parameters, check all lower value primes (which are calculated from previous iterations of the loop) to see if that prime divides into the tested number. If a match is found, the number is not prime (which also cancels the loop for efficiency). The loop also cancels if the prime squared is less than the tested number (as higher primes will be multiplied by smaller numbers, meaning they don't need to be tested.

#### Expanded

function primesBetween(a,b){ var primes=[], test, i, j ; if(b<a){a=[b,b=a][0];} for(i=a;i<=b;i++){ test=true; for(j=0;j<primes.length&&test&&primes[j]*primes[j]<i;j++){ if(i%primes[j]===0){ test=false; } } if(test){ primes.push(i); } } return primes; }

#### Mini

function primesBetween(a,b){var p=[],t,i,j;if(b<a){a=[b,b=a][0];}for(i=a;i<=b;i++){t=true;for(j=0;j<p.length&&t&&p[j]*p[j]<i;j++){if(i%p[j]===0){t=false}}if(t){p.push(i)}}return p;}

#### Usage

var primes = primesBetween(2,10000);