Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

1

113
Vistas
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 Respuestas
Responde la pregunta

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 · 이가현 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda