请查看以下代码
import java.util.ArrayList;
import java.util.List;
public class Big
{
static int primeNumber = 2;
public static void main(String[]args)
{
int numberDevident = 147;
int left=0;
int result=0;
List numbers = new ArrayList();
while(true)
{
result = numberDevident/primeNumber;
left = numberDevident%primeNumber;
if(left!=0)
{
primeNumber++;
}
else
{
numbers.add(primeNumber);
numberDevident = result;
System.out.println(primeNumber);
}
}
}
}
此代码查找给定数字的主要因素(变量" numberDevident"代码")。但是,有一种情况,即给定的数字是600851475143
无法匹配int,long或double。我怎么能用这么大的数字来解决这个问题呢?请帮忙
这里我对BigInteger
做了同样的事情import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class Problem3
{
static BigInteger primeNumber = new BigInteger("2");
static BigInteger zero = new BigInteger("0");
static BigInteger add = new BigInteger("1");
public static void main(String[]args)
{
BigInteger numberDevident = new BigInteger("147");
BigInteger left= new BigInteger("0");;
BigInteger result=new BigInteger("0");;
List numbers = new ArrayList();
while(true)
{
result = numberDevident.divide(primeNumber);
left = numberDevident.remainder(primeNumber);
if(left!=zero)
{
primeNumber.add(add);
}
else
{
numbers.add(primeNumber);
numberDevident = result;
System.out.println(primeNumber.toString());
}
}
}
}
仍然没有好处,它没有显示任何东西。请帮忙。