StackOverflowError computing factorial of a BigInteger?

The problem here looks like its a stack overflow from too much recursion (5000 recursive calls looks like about the right number of calls to blow out a Java call stack) and not a limitation of BigInteger. Rewriting the factorial function iteratively should fix this. For example: public static BigInteger factorial(BigInteger n) { BigInteger result … Read more

PHP: How to convert bigint from int to string?

You actually should ensure that you type what you mean: $bigint = 9999999999999999999; Is not a PHP integer but float: float(1.0E+19) If you would have done $bigint = (int) 9999999999999999999; You would have set an integer in fact, but it would not be the number you might have expected: int(-8446744073709551616) It’s no problem at all … Read more

byte[] to unsigned BigInteger?

The remarks for the BigInteger constructor state that you can make sure any BigInteger created from a byte[] is unsigned if you append a 00 byte to the end of the array before calling the constructor. Note: the BigInteger constructor expects the array to be in little-endian order. Keep that in mind if you expect … Read more

Is there a BigInteger class in PHP?

Hopefully helpfull links : http://php.net/manual/en/ref.bc.php http://php.net/manual/en/ref.gmp.php EDIT: Math_BigInteger Example from http://phpseclib.sourceforge.net/documentation/math.html : Implements an arbitrary precision integer arithmetic library. Uses gmp or bcmath, if available, and an internal implementation, otherwise. <?php include(‘Math/BigInteger.php’); $a = new Math_BigInteger(2); $b = new Math_BigInteger(3); $c = $a->add($b); echo $c->toString(); // outputs 5 ?>

BigInteger: count the number of decimal digits in a scalable method

Here’s a fast method based on Dariusz’s answer: public static int getDigitCount(BigInteger number) { double factor = Math.log(2) / Math.log(10); int digitCount = (int) (factor * number.bitLength() + 1); if (BigInteger.TEN.pow(digitCount – 1).compareTo(number) > 0) { return digitCount – 1; } return digitCount; } The following code tests the numbers 1, 9, 10, 99, 100, … Read more

Logarithm for BigInteger

If you want to support arbitrarily big integers, it’s not safe to just do Math.log(bigInteger.doubleValue()); because this would fail if the argument exceeds the double range (about 2^1024 or 10^308, i.e. more than 300 decimal digits ). Here’s my own class that provides the methods double logBigInteger(BigInteger val); double logBigDecimal(BigDecimal val); BigDecimal expBig(double exponent); BigDecimal … Read more