1 import math 2 3 def primeFactor(source): 4 start = int(math.floor(math.sqrt(source))) 5 6 while start > 1: 7 print start 8 mod = source % start 9 if mod == 0 and isPrime(start): 10 return start 11 start -= 1 12 13 def isPrime(source): 14 start = source / 2 15 16 while start > 1: 17 if source % start == 0: 18 return False 19 start -= 1 20 21 return True 22 23 24 print primeFactor(600851475143)
1 import java.math.BigInteger; 2 3 public class test { 4 public static void main(String[] args) { 5 System.out.println(primeFactor("600851475143")); 6 } 7 8 public static long primeFactor(String source) { 9 BigInteger src = new BigInteger(source); 10 long start = (long) src.divide(new BigInteger("2")).longValue(); 11 12 for (long number = start; number > 0; number--) { 13 System.out.println(number); 14 long module = src.mod(BigInteger.valueOf(number)).longValue(); 15 if (module == 0 && isPrime(number)) 16 return number; 17 } 18 19 return -1; 20 } 21 22 public static boolean isPrime(long source) { 23 long start = (long) Math.floor(source / 2); 24 25 for (long number = start; number > 1; number--) { 26 long module = source % number; 27 if (module == 0) 28 return false; 29 } 30 return true; 31 } 32 }
前边的一分钟查忘记的语法,四分钟写代码,十秒出答案。
后边的写了一个多小时现在还在算。谁告诉我BigInteger怎么开房不开方。