Prime Numbers - Function / array

Asked

Viewed 571 times

-2

Good night, could someone help me understand how the variable works quantityDeducer in the code below? I know she’s storing the prime numbers, but I was wondering if the numbers are stored one at a time or all at once, as an array?

 var n = 10;

 function buscaNumeroPrimos(n) {

  var numerosPrimos = [];

  for (var i = 2; i < n; i++){   //Armazenou os números de 2 a 9
     var quantidadeDeDivisores = 0; 

     for(var b = 2; b < i; b++) { //Armazenou os números de 2 a 9
        if (i % b == 0) {
           quantidadeDeDivisores ++;
        }
     }

     if (quantidadeDeDivisores % i == 0) {
        numerosPrimos.push(i);
     }

  }

return numerosPrimos;
}
  • "...I know she’s storing the prime numbers...", not! quantidadeDeDivisores is not storing the prime numbers, it is actually a counter used to add one to one the number of divisors of i smaller than i. It works as follows, b varies from 2 until i at each variation of b is made a test i % b == 0 if the rest of i divided by b for 0 implies that b is a divider of i and then is added 1 quantidadeDeDivisores. At the end of the iteration if quantidadeDeDivisores is non-zero means that the number is not prime.

  • Thank you very much Augusto.

1 answer

0

Who is storing prime numbers is the method push()

The method push() adds one or more elements at the end of an array and returns the new length of that array.

The symbol of the operator % is called the module operator. The result of an operation with this operator generates the remainder of the division of two values. If the rest is zero, then the first value can be divided exactly by the second.

Arithmetic is useful for those occasions when you want to know if one number is perfectly divisible by another

The variable quantidadeDeDivisores is a variable that quantifies the numbers of divisors of i for b the rest of which is zero 0

Run the code below by clicking Run at the end of this reply and note that when the variable quantidadeDeDivisores is zero means to be a prime number and is stored in the array.

 function buscaNumeroPrimos(n) {

  var numerosPrimos = [];

  for (var i = 2; i < n; i++){   //Armazenou os números de 2 a 9
     var quantidadeDeDivisores = 0; 
     
     var explica = (" Para i = " + i);
     
     for(var b = 2; b < i; b++) { //Armazenou os números de 2 a 9     
        explica += (" ; b = " + b);
        //se o resto dessa divisão for 0 é um divisor e incrementa quantidadeDeDivisores
        if (i % b == 0) {
           quantidadeDeDivisores ++;
        }

     }
        console.log(" ");
        explica +=(" quantidadeDeDivisores = " + quantidadeDeDivisores);
     
 console.log(explica);
 
     //if (quantidadeDeDivisores  == 0) {
     if (quantidadeDeDivisores % i == 0) {
     
        numerosPrimos.push(i);
        console.log(numerosPrimos);
        
     }

  }

return numerosPrimos;

}

buscaNumeroPrimos(20);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.