Prime numbers in javascript

6

I've been surfing the internet, but I can not find an algorithm to search for prime numbers.

var cantidad = 100,j=2;
for(var i=2;i<cantidad;i++) {      
    for(;j<cantidad;j++) {        
        if(j%i==0 && (i==j || i==1)) {          
            console.log(j);          
        }         
    }            
}

I tried the following: Trying from 2, since the prime numbers start from there and in the conditional I practically rely on if the remainder is 0 , and It complies that the divisor is the same number or 1, it would show it by console, but it does not work.

So, what is my mistake and how should it be?

    
asked by Eduardo Sebastian 29.08.2017 в 18:42
source

3 answers

13

Code

var c = 100;
var j = 2;
var numerosPrimos = [];

for (; j < c; j++) {

  if (primo(j)) {
    numerosPrimos.push(j);
  }
  
}

console.log(numerosPrimos);

function primo(numero) {

  for (var i = 2; i < numero; i++) {

    if (numero % i === 0) {
      return false;
    }

  }

  return numero !== 1;
}

Explanation

  

Note: Prime numbers are those that can only be divisible by themselves and by number one.

Therefore, we have created the function:

function primo(numero) {

  for (var i = 2; i < numero; i++) {

    if (numero % i === 0) {
      return false;
    }

  }

  return numero !== 1;
}

That in more natural language does the following:

  

Having a numero we iterate from 2 to the value of numero - 1 and in each iteration we verify if any of those numbers is divisible with numero , if so, we return FALSE otherwise we verify that the number entered is not 1 since this is not considered a prime number. And if the number evaluated was not 1, then we return TRUE, but return FALSE.

Therefore in this loop:

for (; j < c; j++) {

  if (primo(j)) {
    numerosPrimos.push(j);
  }

}

We begin to go through each of the numbers in the iteration by checking through the function primo() if the number is prime and add it to the array.

Update

Due to the comments I have seen, it seems that it has not been clear to you how this algorithm works, I explain it step by step.

Having the following function:

Execute the Snippet to see the operation step by step.

function primo(numero) {

  console.log("Has pasado el numero: " + numero);
  console.log("Inicio bucle desde 2 hasta " + (numero - 1));

  for (var i = 2; i < numero; i++) {

    console.log("Modulo entre " + numero + " y " + i + " = " + (numero % i));

    if (numero % i === 0) {
      console.log(i + " es un multiplo de " + numero);
      console.log(numero + " no es un numero primo porque " + i + " es un multiplo");
      return false;
    }

  }

  if (numero === 1) {
    console.log("Me has pasado el numero 1, recuerda que NO es un numero primo");
  } else {
    console.log("Como el numero ingresado no tuvo mas múltiplos entonces determinamos que SI es un numero primo.");
  }

  console.log("-------------------------------------");
}

primo(2);
primo(4);
primo(5);
primo(10);
    
answered by 29.08.2017 / 18:46
source
0

It would be better to know that numbers in pairs except for 2 is not a prime, we take them out of the formula; we would remove the odd numbers that could be 3, 5, 7, 9 but we know that 9 is multipl of 3 and we issue it, we would only have 3, 5 and 7, as we already know the numbers from 1 to 10, which are the cousins we can omit them and start from 11 adding two in two and so we do not have to validate if it is even

var cantidad = 100,j=2;

function espar( x ) {
  return !( x & 1 );
}
function esMultiplo( x , multiplo ) {
  return (x % multiplo) == 0;
}

for(var i=11;i<cantidad;i+=2) {
    if (esMultiplo(i , 3) || esMultiplo(i , 5) || esMultiplo(i , 7)){
      continue;
   }
  console.log(i)         
}
In this way only use a for, travel fewer numbers from 11 to 100 and the results are the desired     
answered by 31.10.2018 в 14:20
-2
function esPrimo(n) {
  let i = 1;
  while (n % ++i != 0);
  return n == i;
}

for (let i = 2; i < 100; i++)
  if (esPrimo(i))
    console.log(i);
    
answered by 31.10.2018 в 13:36