Problem 3. Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

import java.util.ArrayList;
import java.util.Iterator;

public class euler3 {
    public static void main(String[] args) {
        long N = 600851475143L;
        long k = 1L;
        long l = 2L;
        double i;
        ArrayList<Long> a = new ArrayList<Long>();
        for (i = N/2.0; i > 1; i=i*k/l) {
            k++;
            l++;
            long j = (long)i;
            if (N % j == 0) {
                if (isPrime(j)) {
                    a.add(j);
                    break;
                }
            }
        }
        System.out.println(a.get(0));
    }
    public static boolean isPrime(long n) {
        if (n <= 1) return false;
        long i = 2;
        while (i*i <= n) {
            if (n % i == 0)
                return false;
            i++;
        }
        return true;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>