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

biginteger on Objective-c

I hope it’s not too late to answer this thread. You can try “LibTomMath” which is opensource and free (the author give away this project as public domain). It works out of the box without any configuration, just put all bn_*.c and tommath*.h to your Xcode project and you are ready to go. #import “tommath.h” … Read more

How do I do big integers in Fortran?

There is no built-in “big number” support, but we can first check whether there is a larger integer kind available (as mentioned by Francescalus above and also many previous pages (e.g. this page). On my computer with gfortran-6.1, the compiler seems to support 128-bit integer kind, so I could calculate the result up to n=160 … Read more

Often big numbers become negative

This image shows what you’re looking for. In your case it’s obviously larger numbers, but the principle stays the same. Examples of limits in java are: int: −2,147,483,648 to 2,147,483,647. long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 In the image 0000, 0001 etc, shows the binary representation of the numbers. EDIT: In project euler you often have to … Read more

How can I find the Square Root of a Java BigInteger?

Just for fun: public static BigInteger sqrt(BigInteger x) { BigInteger div = BigInteger.ZERO.setBit(x.bitLength()/2); BigInteger div2 = div; // Loop until we hit the same value twice in a row, or wind // up alternating. for(;;) { BigInteger y = div.add(x.divide(div)).shiftRight(1); if (y.equals(div) || y.equals(div2)) return y; div2 = div; div = y; } }

tech