public class Euler510 {
    private static long gcd(long a, long b) {
        while (b != 0) {
            long t = b;
            b = a % b;
            a = t;
        }
        return a;
    }

    private static long sumFormula(long nMax) {
        long total = 0;
        for (long n = 1;; ++n) {
            long minB = (n + 1) * (n + 1) * n * n;
            if (minB > nMax)
                break;

            for (long m = 1; m <= n; ++m) {
                if (gcd(m, n) != 1)
                    continue;

                long s = m + n;
                long b0 = s * s * n * n;
                if (b0 > nMax)
                    break;

                long a0 = s * s * m * m;
                long c0 = m * m * n * n;

                long tMax = nMax / b0;
                long tSum = tMax * (tMax + 1) / 2;
                total += (a0 + b0 + c0) * tSum;
            }
        }
        return total;
    }

    public static void main(String[] args) {
        long nMax = 1000000000L;
        System.out.println(sumFormula(nMax));
    }
}
