Saturday, January 14, 2012

Problem 3

Problem Statement : Problem 3

Algorithm:
We try to iterate over all the factors of the number.
The algorithm runs over all prime factors of a given number that are less than N^.5.

Solution:
public class problem3 {
 public static boolean isPrime(double num) {
  for (int i = 2; i <= Math.sqrt(num); i++) {
   if (num % i == 0)
    return false;
  }
  return true;
 }

 public static void main(String rgp[]) {
  long num = (long) 600851475143.0;
  
  for (int i = (int)Math.pow(num,.5);  i>=3; i--) {
   if (num % i == 0) {
    if (isPrime(i)) {
     System.out.println(i);
     break;
    }
   }
  }
 }
}

No comments:

Post a Comment