Large Numbers in Java
240,784
Solution 1
You can use the BigInteger
class for integers and BigDecimal
for numbers with decimal digits. Both classes are defined in java.math
package.
Example:
BigInteger reallyBig = new BigInteger("1234567890123456890");
BigInteger notSoBig = new BigInteger("2743561234");
reallyBig = reallyBig.add(notSoBig);
Solution 2
Use the BigInteger
class that is a part of the Java library.
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html
Solution 3
Here is an example which gets big numbers very quickly.
import java.math.BigInteger;
/*
250000th fib # is: 36356117010939561826426 .... 10243516470957309231046875
Time to compute: 3.5 seconds.
1000000th fib # is: 1953282128707757731632 .... 93411568996526838242546875
Time to compute: 58.1 seconds.
*/
public class Main {
public static void main(String... args) {
int place = args.length > 0 ? Integer.parseInt(args[0]) : 250 * 1000;
long start = System.nanoTime();
BigInteger fibNumber = fib(place);
long time = System.nanoTime() - start;
System.out.println(place + "th fib # is: " + fibNumber);
System.out.printf("Time to compute: %5.1f seconds.%n", time / 1.0e9);
}
private static BigInteger fib(int place) {
BigInteger a = new BigInteger("0");
BigInteger b = new BigInteger("1");
while (place-- > 1) {
BigInteger t = b;
b = a.add(b);
a = t;
}
return b;
}
}
Solution 4
Checkout BigDecimal
and BigInteger
.
Solution 5
import java.math.BigInteger;
import java.util.*;
class A
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.print("Enter The First Number= ");
String a=in.next();
System.out.print("Enter The Second Number= ");
String b=in.next();
BigInteger obj=new BigInteger(a);
BigInteger obj1=new BigInteger(b);
System.out.println("Sum="+obj.add(obj1));
}
}
Related videos on Youtube
Comments
-
Petey B over 2 years
How would I go about doing calculations with extremely large numbers in Java?
I have tried
long
but that maxes out at 9223372036854775807, and when using an integer it does not save enough digits and therefore is not accurate enough for what I need.Is there anyway around this?
-
Jin Kwon over 7 years
9223372036854775807
is the exact value ofLong.MAX_VALUE
, anyway.
-
-
haylem almost 11 yearsMight be worth mentioning the (though obvious for most, I guess) inherit performance hit incurred by the use of the BigInteger class if you plan to do calculations with that.
-
Zubin Mukerjee about 6 yearsFor really large Fibonacci numbers, the recursive calculation is ridiculously time consuming. Much better to use Binet's explicit formula. A few Math.pow()s and Math.sqrt()s later, you're done! :)
-
Vishy about 6 years@ZubinMukerjee however pow and sqrt on BigDecimal isn't cheap either. It is better than iteration but not as simple as it sounds.
-
Eboubaker over 3 years@haylem the performance speed is the same but the length of the number makes it takes time. they use bitwise operators to do the calculations. like what happens normally when doing math with primitive types.