In java, how to calculate the factorial of large numbers, say 70 and above? I made a program but the largest factorial number that I can calculate is just 69.
Depends on why yours is breaking down. Are you exceeding the space for the type of integer you are using?
I just use the int data type. Maybe I should try long data type
Yah, you may be running into integer overflow: http://en.wikipedia.org/wiki/Integer_overflow And other wonderful issues with large numbers.
When I enter large numbers, it displays only 0. But I get it now. Thanks for reminding me about integer overflow.
np. have fun!
If you run into overflow problems with the Long, you should try BigInteger. It takes longer to process operations on BigIntegers, but they can be arbitrarily large. It can represent numbers larger than you'd ever hope to use outside of some very specific fields (you'd have a very hard time trying to fit Graham's Number into a BigInteger, for example).
how to use it? (sample code)
BigInteger a = new BigInteger("20"); // or any value... I think this needs to be a string // or a byte array BigInteger b = new BigInteger("10"); a = a.multiply(b); // a is now 200 a = a.add(b); //a is now 210 If you're using eclipse, you can just type "a." then look at the methods that come up.
There is an example of that in the StackOverflow I linked... That was why I chose it. hehe.
Yes, I saw it. I just want to see his example :))
Join our real-time social learning platform and learn together with your friends!