问题:我无法存储号码“600851475143”。我意识到这个数字大于 int 可以容纳的数字,并且小于最大 long 值。但是,我的程序没有将变量“number”注册为 long,而是将其注册为 int。有人可以阐明这个问题吗?
** - 问题所在行
public class Problem3{
//What is the largest prime factor of the number 600851475143
public static void main(String[] args){
***long number = 600851475143 , total = 0;
for(long x = (number-1)/2; x>1; x--)
if(number%x == 0 && isPrime(x)) total += x;
System.out.println(total);
}
private static boolean isPrime(long determine){
for(long x = determine/2 - 1; x>1; x--)
if(determine%x ==0) return false;
return true;
}
}
解决方案:正如吉姆在下面所说,为了输入长,必须在数字的末尾加上“L”或“l”。"整数文字如果以字母 L 或 l 结尾,则为 long 类型;否则为 int 类型。建议您使用大写字母 L,因为小写字母 l 与数字 1 难以区分。” - 来自 Oracle 网站上的原始类型。
更多信息: Java 的 L 编号(长)规范