Project-Euler / 12

Problem 12 asks:

What is the value of the first triangle number to have over five hundred divisors?

Here’s my solution in Java:

public class Problem12 {
    public static void main(String[] args) {
        int triangle = 0;
        for (int n = 1, numFactors = 0; numFactors <= 500; n++) {
            triangle = n * (n + 1) / 2;
            numFactors = 2; 
            for (int div = 2; div * div <= triangle; div++) {
                if (triangle % div == 0) numFactors += 2;
            }
        }
        System.out.println(triangle);
    }
}

Last updated on .