I'm writing a Java program to do some computation on big prime numbers, I get this error:
我正在编写一个Java程序来对大质数进行一些计算,我得到了这个错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1.0" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:492) at java.math.BigInteger.<init>(BigInteger.java:338) at java.math.BigInteger.<init>(BigInteger.java:476) at Solution.sumOfDivisorsModulo(Solution.java:24) at Solution.main(Solution.java:49)
线程“main”中的异常java.lang.NumberFormatException:对于java.lang.Integer.parseInt(Integer.java:492)中java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)的输入字符串:“1.0” java.math.BigInteger。
public static BigInteger sumOfDivisorsModulo(BigInteger n){
BigInteger sum = (n.add(one)).mod( MODULO);
for (BigInteger test = n.subtract(one); test.compareTo(new BigInteger(Double.toString(Math.sqrt(n.longValue())))) >= 0; test.subtract(one))
{
if(n.mod(test).compareTo(zero) == 0)
{
sum = sum.add(test);
sum = sum.add(n.divide(test));
sum = sum.mod(MODULO);
}
}
return sum;
}
public static void main(String[] args) {
int m = 2;
int a = 0;
primeList = new BigInteger[m];
fillList(m); // fills the list primeList with prime number up to the mth
BigInteger n = new BigInteger("1");
for (int i = 0; i < m; i++){
n.multiply(primeList[i].pow(a+i));
}
System.out.println(sumOfDivisorsModulo(n).toString()); // base10
}
one
and zero
are variables defined as BigInteger("0")
and BigInteger("1")
. Can you help me figure out what the problem is? I thank you in advance.
一个和零是变量,定义为BigInteger(“0”)和BigInteger(“1”)。你能帮我搞清楚问题是什么吗?我提前谢谢你。
1 个解决方案
#1
1
The problem is here.
问题出在这里。
new BigInteger(Double.toString(Math.sqrt(n.longValue())))
The Double.toString()
call is going to give you a number string with a decimal point in it. But the BigInteger(String)
constructor cannot parse a number string with a decimal point in it.
Double.toString()调用将为您提供一个带小数点的数字字符串。但是BigInteger(String)构造函数无法解析带有小数点的数字字符串。
I don't understand what you are trying to do here, but the square root is liable to be a non-integer value.
我不明白你在这里想做什么,但平方根可能是一个非整数值。
If your intention is to convert the floating point (possibly non-integer) square-root value to an integer, then:
如果您打算将浮点(可能是非整数)平方根值转换为整数,那么:
// Round towards zero / truncate
BigInteger.valueOf((long)(Math.sqrt(n.longValue())))
or
// Round to nearest
BigInteger.valueOf((long)(Math.round(Math.sqrt(n.longValue()))))
This should be more efficient than going via a string. And going via an int
string is liable to overflow sooner.
这应该比通过字符串更有效。通过int字符串很快就会溢出。
Note however that for large enough values of n
the square-root calculation will be noticeably inaccurate. There is no solution apart from finding or implementing your own BigInteger
square-root method. However, if @Andreas is correct and you don't need to use BigInteger
at all, this is moot.
但请注意,对于足够大的n值,平方根计算将明显不准确。除了查找或实现您自己的BigInteger平方根方法之外,没有其他解决方案。但是,如果@Andreas是正确的,你根本不需要使用BigInteger,这是没有实际意义的。
#1
1
The problem is here.
问题出在这里。
new BigInteger(Double.toString(Math.sqrt(n.longValue())))
The Double.toString()
call is going to give you a number string with a decimal point in it. But the BigInteger(String)
constructor cannot parse a number string with a decimal point in it.
Double.toString()调用将为您提供一个带小数点的数字字符串。但是BigInteger(String)构造函数无法解析带有小数点的数字字符串。
I don't understand what you are trying to do here, but the square root is liable to be a non-integer value.
我不明白你在这里想做什么,但平方根可能是一个非整数值。
If your intention is to convert the floating point (possibly non-integer) square-root value to an integer, then:
如果您打算将浮点(可能是非整数)平方根值转换为整数,那么:
// Round towards zero / truncate
BigInteger.valueOf((long)(Math.sqrt(n.longValue())))
or
// Round to nearest
BigInteger.valueOf((long)(Math.round(Math.sqrt(n.longValue()))))
This should be more efficient than going via a string. And going via an int
string is liable to overflow sooner.
这应该比通过字符串更有效。通过int字符串很快就会溢出。
Note however that for large enough values of n
the square-root calculation will be noticeably inaccurate. There is no solution apart from finding or implementing your own BigInteger
square-root method. However, if @Andreas is correct and you don't need to use BigInteger
at all, this is moot.
但请注意,对于足够大的n值,平方根计算将明显不准确。除了查找或实现您自己的BigInteger平方根方法之外,没有其他解决方案。但是,如果@Andreas是正确的,你根本不需要使用BigInteger,这是没有实际意义的。