Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Large Integer mod calculation

I need to calculate modulus with large number like :

<?php

    $largenum = 95635000009453274121700;

    echo $largenum % 97;

?>

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

Any idea how to do this ?

like image 282
Kami Avatar asked Apr 17 '09 10:04

Kami


1 Answers

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.

like image 57
vartec Avatar answered Nov 15 '22 22:11

vartec