7

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
Pentium10
  • 204,586
  • 122
  • 423
  • 502

4 Answers4

8

You can go for BCMath to work with big numbers.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
4

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
neubert
  • 15,947
  • 24
  • 120
  • 212
1

There are many options:

castarco
  • 1,368
  • 2
  • 17
  • 33
1

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 :)

BenMorel
  • 34,448
  • 50
  • 182
  • 322