1

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.

Thabmias
  • 19
  • 1
  • My suspicion is that it has to do with order of operations in `(y * y > z)` – C_Z_ Jul 31 '22 at 22:44
  • 1
    first code doesn't respect javasvript syntax , you may use strict mode https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode – Mister Jojo Jul 31 '22 at 22:56
  • Does this answer your question? [What does "use strict" do in JavaScript, and what is the reasoning behind it?](https://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it) – Mister Jojo Jul 31 '22 at 23:12

2 Answers2

3

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.

muell
  • 383
  • 1
  • 15
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

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40