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

0

161
Vistas
Identical function leads to different outcomes in different languages

I wrote a function in javascript that is supposed to return the lowest prime number larger than x. Instead it get's caught in indefinite recursion.

function next_prime(x) {
    if (x <= 1) {
        return 2;
    }
    y = 2;
    z = x + 1;
    while(true) {
        if (z % y == 0) {
            z++;
            y = 2;
            continue;
        }
        if(y * y > z) {
            return z;
        }
        y = next_prime(y);
    }
}

I didn't understand what was wrong, so I implemented the same function in python, where it worked just fine.

def next_prime(x):
    if x <= 1:
        return 2
    y = 2
    z = x + 1
    while True:
        if z % y == 0:
            z += 1
            y = 2
            continue
        if y * y > z:
            return z
        y = next_prime(y)

I looked over both functions and I'm sure that they are identical, yet in python it works and in javascript it doesn't.

Allthough appreciated, I'm not necessarily looking for help with this specific problem, I'm more so interested in what is actually going on here.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I think the problem is that you are not declaring your variables correctly. In JavaScript, you have to use the keywords let and const - which stand for reassignable and not reassignable variables - when declaring a new variable. When using let in lines five and six, the function works just fine.

about 4 years ago · Santiago Trujillo Denunciar

0

You should use strict mode to avoid this common mistake of javascript beginners.

your actual js code with 'use strict'; is rejected :

'use strict';

console.log( next_prime(7) )  // never run on strict mode, otherwise make an infinite loop

function next_prime(x) {
  if (x <= 1) {
    return 2;
  }
  y = 2;
  z = x + 1;
  while(true) {
    if (z % y == 0) {
        z++;
        y = 2;
        continue;
    }
    if(y * y > z) {
        return z;
    }
    y = next_prime(y);
  }
}

same code in correct javascript syntax :

'use strict';

console.log( next_prime(7) )  // -> 11

function next_prime(x)
  {
  if (x <= 1)  return 2
    ;
  let y = 2, z = x + 1 // declare y and z as locale
    ;
  while(true)
    {
    if (z % y == 0) 
      {
      z++;
      y = 2;
      continue;
      }
    if (y**2 > z)  return z
      ;
    y = next_prime(y);
    }
  }

explanation : your code is recursive, inside call will change y and z value on parent callers and make an infinite loop

about 4 years ago · Santiago Trujillo 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