-1

The bcmod function is deactivated and I won't be able to activate this because its not my own server.

For a reason I need to calculate an integer with a length of atleast 24 with modulo 97. Integer cant be that long, thats why it can't work...

I already tried it with simple operator "%" and the "fcmod" lib but this returns me completely wrong numbers.

Does someone know if I can solve this by my own without any other libraries or do I really need "bcmod"?

This would be the code as example:

123456789101112131415171%97 The real answer would be 96 but it returns me -94

ShUtDoWn
  • 75
  • 2
  • 7
  • 1
    It may be useful of you show us what you are actually doing and then maybe we can be of some assistance – RiggsFolly May 13 '22 at 15:33
  • 1
    No, show us the code you are currently using to get the right answer – RiggsFolly May 13 '22 at 15:45
  • Thats exactly what I need. For real... I just need to modulo a really big number – ShUtDoWn May 13 '22 at 15:45
  • 1
    Is this any help https://stackoverflow.com/questions/2490888/how-to-work-with-big-numbers-in-php – RiggsFolly May 13 '22 at 15:54
  • You know with [gmp](http://php.net/gmp) get this for free. For example `$num = gmp_init("123456789101112131415171"); echo $num % 97;` basically all you got to do is initialize the gmp objects once and basically php will automagically use it as an arbitrary precision number in any standard mathmatical operation (as long as atleast one operand is a gmp object) – Sherif May 13 '22 at 16:23
  • tried now, but gmp and bcmod are both deactivated :/ – ShUtDoWn May 13 '22 at 16:28
  • Does this answer your question? [Is there a BigInteger class in PHP?](https://stackoverflow.com/questions/4427020/is-there-a-biginteger-class-in-php) – Progman May 13 '22 at 17:16

1 Answers1

2

If you've got a number that is too big you can divide the problem into several steps with smaller numbers. An example:

Suppose we do this in steps of 3 digits. So, 1550 % 16 is the same as first doing 155 % 16, which is 11. After this first step we combine the result with what was left of the whole number. That is: 11 and 0 which gives 110. We are now left with only 3 digits, so we do 110 % 16 which is 14. So the result is 14.

We can implement this in a function, I use 8 digits at once, because it is quicker:

function modulo($value, $modulo)
{
    while ((strlen($value) > strlen($modulo)) || 
           (intval($value) >= $modulo)) {
        $head = substr($value, 0, 8);
        $tail = substr($value, 8);
        $value = ($head % $modulo) . $tail;
    }    
    return $value;
}

Now if we do:

$value = "123456789101112131415171";
$modulo = 97;
echo modulo($value, $modulo);

We get 96. Note how the big integer is a string, otherwise it won't work. There are a lot of implicit type conversions going on in this function.

See: PHP Sandbox.

A shorter version is possible, but is functionally the same:

function modulo($value, $modulo)
{
    while ((strlen($value) > strlen($modulo)) || 
           (intval($value) >= $modulo)) {
        $value = (substr($value, 0, 8) % $modulo) . substr($value, 8);
    }    
    return $value;
}

See: PHP Sandbox.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33