import java.math.BigInteger;
/*
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
*/
public class Problem3 {
long solve(long x) {
long i;
for(i = 2; i <= x; i++) {
if(x % i == 0) {
break;
}
}
return x == i? x: solve(x / i);
}
public static void main(String[] args) {
// the largest prime factor of 600851475143 = 6857;
System.out.println(new Problem3().solve(Long.valueOf("600851475143")));
}
}
비밀개츄
내가 생각해도 elegant 하게 잘 품..