Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!