加了L才是long,不加L是int。
1 public class test { 2 public static void main(String[] args) { 3 System.out.println(primeFactor(600851475143L)); 4 } 5 6 public static long primeFactor(long source) { 7 long start = (long) Math.floor(Math.sqrt(source)); 8 9 for (long number = start; number > 0; number--) { 10 System.out.println(number); 11 long module = source % number; 12 if (module == 0 && isPrime(number)) 13 return number; 14 } 15 return -1; 16 } 17 18 public static boolean isPrime(long source) { 19 long start = (long) Math.floor(source / 2); 20 21 for (long number = start; number > 1; number--) { 22 long module = source % number; 23 if (module == 0) 24 return false; 25 } 26 return true; 27 } 28 }