Project Euler / 3

Problem 3 asks:

What is the largest prime factor of the number 600 851 475 143?

Here’s what I did in Java:

public class Problem3 {
    public static void main(String[] args) {
        long number = 600851475143L;
        int result = 0;
        for (int factor = 2; factor <= number; factor++) {
            if (number % factor == 0) {
                result = factor;
                number /= factor;
                factor = 2;
            }
        }
        System.out.println(result);
    }
}

This required a little more thought to write efficiently, but my solution turned out very simple so it’s easy to follow.

Last updated on .