Pages

Tuesday, September 8, 2009

Euler Problem 3 solution

Time (s): ~0.001
package margusmartseppcode.From_1_to_9;

public class Problem_3 {
 static long lpf(long nr) {
  long max = 0;

  for (long i = 2; i <= nr / i; i++)
   while (nr % i == 0) {
    max = i;
    nr = nr / i;
   }

  return nr > 1 ? nr : max;
 }

 public static void main(String[] args) {
  System.out.println(lpf(600851475143L));
 }
}

No comments:

Post a Comment