Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

1

122
Visualizações
How many prime numbers from 2 to 100000 javascript?

I am new to JS. I am trying to write a program where in I need to find the prime numbers between set of 2 numbers. When I run this on my IDE I get "Runtime Error - Other". No pressing debug on the IDE I get "Using long recursion is taking long"

Here is the program

var num1 = 2;
var num2 = 10;

var primeArray = [];

for(i=num1; i<=num2; i++){
    if(checkPrime(i)){
    primeArray.push(i)    
    }
}

console.log(primeArray.join(" "))

// console.log(checkPrime(10));

function checkPrime (num){
    //convert number to string and get each digit in string form
    var numString = num.toString().split("");
    //parse each string element to convert to number
    var numArray = numString.map(function(i){
        return parseFloat(i)
    });
    //sum all digits
    var sum = 0;
    for(i=0; i<numArray.length; i++){
        sum= sum+ numArray[i];
    }
    //2 is prime
    if(num == 2){
        return true;
    } else if(num%2 === 0){ //if even then not prime
        return false;
    } else if(sum == 3){ // 3 is prime
        return true;
    } else if(sum%3 === 0){ // if sum of all digits is divisible by 3 then not prime 
        return false;
    } else{
        return true;
    }
}  

I am not able to figure out where I am going wrong

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

var num1 = 2;
var num2 = 10;

var primeArray = [];

for(i=num1; i<=num2; i++){
    if(checkPrime(i)){
    primeArray.push(i)    
    }
}

console.log(primeArray.join(" "))

// console.log(checkPrime(10));

function checkPrime (num){
    //convert number to string and get each digit in string form
    var numString = num.toString().split("");
    //parse each string element to convert to number
    var numArray = numString.map(function(i){
        return parseFloat(i)
    });
    //sum all digits
    var sum = 0;
    for(i=0; i<numArray.length; i++){
        sum= sum+ numArray[i];
    }
    //2 is prime
    if(num == 2){
        return true;
    } else if(num%2 === 0){ //if even then not prime
        return false;
    } else if(sum == 3){ // 3 is prime
        return true;
    } else if(sum%3 === 0){ // if sum of all digits is divisible by 3 then not prime 
        return false;
    } else{
        return true;
    }
}  

about 4 years ago · 이가현 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda