Why Modular Exponentiation functions work differently in Python and Javascript for large numbers?
<p>I need to perform modular exponentiation on quite large numbers on both python3 and javascript. I have functions that do the task, but they give me different outputs.</p>

<p>Python (all of the three work in the same way):</p>

<pre><code>pow(176672119508, 55, 200000023499)

def expmod_iter(a,b,c):
    x = 1
    while(b&gt;0):
        if(b&amp;1==1): x = (x*a)%c
        a=(a*a)%c
        b &gt;&gt;= 1
    return x%c

def pow_mod(x, y, z):
    number = 1
    while y:
        if y &amp; 1:
            number = number * x % z
        y &gt;&gt;= 1
        x = x * x % z
    return number

# The result is always 124912252967
</code></pre>

<p>and now JavaScript (both functions work in the same way):</p>

<pre><code>function powMod(x, y, z) {
  let number = 1;
  while (y) {
      if (y &amp; 1) {
          number = number * x % z;
      }
      y &gt;&gt;= 1;
      x = x * x % z;
  }
  return number;
}

function expmod_iter(a, b, c) {
  let x = 1;

  while (b &gt; 0) {
    if (b &amp; 1 === 1) {
      x = (x * a) % c;
    }
    a = (a * a) % c;
    b &gt;&gt;= 1
  }
  return x % c;
}

console.log(powMod(176672119508, 55, 200000023499));
console.log(expmod_iter(176672119508, 55, 200000023499));

// The result is always 138693107570
</code></pre>

<p>And moreover, when I used <a href="https://www.mtholyoke.edu/courses/quenell/s2003/ma139/js/powermod.html" rel="nofollow">this service</a> with my numbers, I also got 138693107570.</p>

<hr>

<p>Why does this happen? I'm not even sure what variant is correct now. However, on smaller numbers the functions give identical results.</p>

<p>Is it possible to somehow get the same result from the functions? It doesn't even matter that much that the result is mathematically correct, the results should be at least the same.</p>

<p>Could you please explain why this happens? Is it the function design? To me, functions on both languages seem to be identical.</p>

<p>Is there a way to get the same result from functions of both languages?</p>