我在Eclipse中编写代码以查找给定数字的素因子。当我输入较小数量的Long类型时,我能够获得具有所有素因子的输出。但是,当我使用大数(试图为项目Euler做600851475143L)时,控制台窗口不显示,当我手动显示窗口时,没有输出。我该怎么做才能显示结果?下面是我的代码,以防它引起问题,输入的数字在主方法中。我该怎么做才能显示输出?
import java.util.*;
public class Prime {
public static List<Long> getFactors(Long number) {
List<Long> factors = new ArrayList<Long>();
for (Long i = (long) 2; i < number/2 + 1; i++) {
if (number % i == 0 ) {
factors.add(i);
}
}
return factors;
}
public static void getPrimeFactors(Long x) {
List<Long> primes = new ArrayList<Long>();
for (int n = 0; n < getFactors(x).size(); n++) {
boolean prime = true;
Long y = getFactors(x).get(n);
int factor = 2;
while ( prime == true && factor < y/2 + 1) {
if (y % factor == 0) {
prime = false;
}
else {
factor++;
}
}
if (prime == true) {
primes.add(y);
}
}
for (int i = 0; i < primes.size(); i++) {
System.out.println(primes.get(i));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Long y = 600851475143L;
getPrimeFactors(y);
}
}