Home » Php » biginteger – How to work with big numbers in PHP?

biginteger – How to work with big numbers in PHP?

Posted by: admin July 12, 2020 Leave a comment

Questions:

How to work with big numbers in PHP?

such as

(6*27^0+17*27^1+11*27^2+18*27^3+25*27^4+4*27^5)^65537
How to&Answers:

You can go for BCMath to work with big numbers.

Answer:

GMP’s actually faster than BCMath for bigintegers if you have it installed. If you have neither BCMath or GMP installed you can use phpseclib’s pure-php biginteger implementation.

That implementation uses GMP or BCmath if they’re available, in that order, and it’s own internal implementation otherwise.

Answer:

There are many options:

Answer:

Given the question title, I’m assuming that the OP meant ^ as a power operator and not PHP’s XOR operator, although the actual numbers make me doubt.

This can be achieved using the Brick\Math library (disclaimer: I authored it):

use Brick\Math\BigInteger;

// Not using BigInteger just yet as the numbers are small, although we could
$value =  6 * 27 ** 0
       + 17 * 27 ** 1
       + 11 * 27 ** 2
       + 18 * 27 ** 3
       + 25 * 27 ** 4
       +  4 * 27 ** 5;

echo BigInteger::of($value)->power(65537); // 529970661615774734826076722083948398443...

I’m sparing you the rest of the 514566 digits 🙂