7

Is there are trick to get the modulo of big numbers in Javascript. I am getting infinity with modulo(7, 16971, 25777) 7^16971mod25777=NaN

function modulo (n, p, m){
var x = Math.pow(n, p);
var y = m;
var z = x%y;
alert(x);
return z;
}
Amit
  • 45,440
  • 9
  • 78
  • 110
  • 2
    You're looking for [*modular exponentiation*](https://en.wikipedia.org/wiki/Modular_exponentiation), and it's not specific to JavaScript. – Bergi Oct 16 '16 at 21:27
  • 1
    @Lưu Vĩnh Phúc Why is this duplicate? it's tagged javascript –  Oct 17 '16 at 05:10
  • because the algorithm is language-agnostic, it's pure math. – Bergi Oct 17 '16 at 11:10

4 Answers4

9

There's a mathematical "trick" you can use, if you can assume all parameters are integers.

Consider the following modulo operation:

(a*x + y) % x

Obviously, the a*x part can be discarded and the following holds:

(a*x + y) % x = y % x

With that in mind we can assume the big number is just a*x + y, and we can perform the modulo at any stage, and as often as we like, so, to get the result you want, do this:

function modulo (n, p, m){
  var result = 1;
  while(p--) {
    result = (result * n) % m;
  }
  
  return result;
}

console.log(modulo(7, 16971, 25777));
Amit
  • 45,440
  • 9
  • 78
  • 110
3

JavaScript numbers are stored as 64-bit floats.

Math.pow(7, 16971) is Infinity because the value is too large for that representation. Specifically, it's larger than Number.MAX_VALUE, which is 1.7976931348623157e+308.

The largest safe integer is Math.pow(2, 53) - 1), aka Number.MAX_SAFE_INTEGER.

You can use an arbitrary size integer library like big-integer to work with larger integers:

const result = bigInt(7).modPow(16971, 25777);
console.log(result.value); // 857

JSFiddle

joews
  • 29,767
  • 10
  • 79
  • 91
1

You'll probably want to look into a large number library such as big.js to do this. It has its own mod() function to handle larger numbers and greater floating point precision.

From the manual:

1 % 0.9                    // 0.09999999999999998
x = new Big(1)
x.mod(0.9)                 // '0.1'
Soviut
  • 88,194
  • 49
  • 192
  • 260
1

Please try this, it should work for you...

 <script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>
<script>
    function modulo(n, p, m) {
        var x = bigInt(n).pow(p);
        var y = m;
        var z = bigInt(x).mod(y);
        alert(x);
        alert(z);
        return z;
    }
    modulo(7, 16971, 25777);

</script>

Value of X= ( 144157446840451635235083706110907852415749228859252529148906391999766994677256648514596635518338118874745245599504027645569205474259056773767697690363704468632892152795016715055324575445087682781252313005869045568884109150825799944546337893064300709178398146710515468212610079448225972249066488499049225372747076806433631659786194988344294497773759564575000162869574365014937829611100108282508068839769488427218809418476143641444334160948843097387146975458980549194883596975058014553601039150039974922599124812752683319818785474747861041069869797998022819369652619759825244859686407688179575508679861543683676353692931928781365284923967762962761189903683793268647203089135578161089792845634056425105473120490657724974694040110140134504449715061852058159494813855440466218772852172975097582562908895057311050472869260715192269051794091102837753073541384982827121618414372575452344004360364276677087398549812260325448141226947881328515773351976616276417638128022815680053293310617319251468387901625157...56951333749257599033126883342183151178668919812064049965349560466150682525651094508048667165975539000764644172767648163518366194953573817885103167718630743142062623550549541359220427411352708364483389060986844929269143259135008252906461288098421933603373774514126347477000279431329468363160423511545129487503178839098880369937328996412126931687097210220191726087729442555830870326323512951767388505151559227624666317971526350895004302090730198002124799887057180493028281166853990182770936726392403645367304961828645095221020100469965292184204520213166368848723223621651107654075062116217744242552262031457878341343131239324794711518591327361143916482110866686618572491075943511233044928342441933757654662089762470943194596874717623496819342403306038522266428198018364568515908102686200233757394776127456240030822204960242512397946554388855232832783930954979762030089547004776120626513910030444279665047610388454114197939348310563226006027400434616239674784018828580353008938225035036985223336494743), please take a look at output screen below.

enter image description here

Value of Z=(857). please take a look at output screen below.

enter image description here

Bhanu Pratap
  • 1,635
  • 17
  • 17