Friday, April 29, 2011

PHP - Large Interger mod calculation

I need to calculate modulus with large number like :

<?php

    $largenum = 95635000009453274121700;

    echo $largenum % 97;

?>

It's not working... beacause $largenum is too big for an int in PHP.

Any idea how to do this ?

From stackoverflow
  • Use bcmod() from BCMath Arbitrary Precision Mathematics:

    $largenum = '95635000009453274121700';
    echo bcmod($largenum, '97');
    

    Note, that $largenum is passed as a string, not converted to int.

  • vartec,

    you are a genius. I was looking anywhere for a solution like this. Thanks a lot. ;)

0 comments:

Post a Comment