How to work with big numbers in PHP?

这一生的挚爱 提交于 2019-11-29 07:18:56

You can go for BCMath to work with big numbers.

neubert

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.

There are many options:

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!