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

0

162
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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