The most common definition is that "a number is prime if it is divisible by 1 and by itself" or else "it is all the number with two and only two divisors, itself and the unit". So, for example, the number 7 is prime because it is divisible only by 1 and by 7.
- By definition the number 2 is a prime number so we must test from the divisor
2
.
- If in function
primeNumber
there is some divisor that the rest of the division is equal to 0
he’s not cousin and we can stop with the check.
function primeNumber(num) {
for (var divisor = 2; divisor < num; divisor++)
if (num % divisor == 0) return false;
return true;
}
var determinadoNumero = 50;
for (var i = 2; i < determinadoNumero; i++) if (primeNumber(i)) console.log(i);
the number 2
is the only prime number that is even.
The rest of the division of a number by 1, or the rest of the data by itself, will always be zero. Review the definition of prime number and its algorithm.
– anonimo