in python with a simple loop you can calculate, let's say 600!
it's a very very big number but python can easily take care of it in a fraction of a second.even it's more than 200
digit long. in java in the other hand you are bound to 64bit literals (long data type). so the machine will return 0.
is there any way to overcome this?
在python中有一个简单的循环,你可以计算,让我们说600!这是一个非常大的数字,但python可以在几分之一秒内轻松处理它。即使它超过200位数。另一方面,在java中你必须使用64位文字(长数据类型)。所以机器将返回0.有没有办法克服这个?
1 个解决方案
#1
51
You can use the Java BigInteger
class.
您可以使用Java BigInteger类。
And a simple example:
还有一个简单的例子:
import java.math.BigInteger;
BigInteger k = BigInteger.valueOf(10000L);
k = k.pow(10000);
//k is now 10000^10000
System.out.println(k.toString());
It's important to know that the class is immutable. You can also look into the similar BigDecimal
class for arbitrary precision signed decimal numbers.
知道该类是不可变的很重要。您还可以查看类似的BigDecimal类,以获取任意精度带符号的十进制数。
#1
51
You can use the Java BigInteger
class.
您可以使用Java BigInteger类。
And a simple example:
还有一个简单的例子:
import java.math.BigInteger;
BigInteger k = BigInteger.valueOf(10000L);
k = k.pow(10000);
//k is now 10000^10000
System.out.println(k.toString());
It's important to know that the class is immutable. You can also look into the similar BigDecimal
class for arbitrary precision signed decimal numbers.
知道该类是不可变的很重要。您还可以查看类似的BigDecimal类,以获取任意精度带符号的十进制数。