import java.io.*; import java.math.BigInteger; public class prob120 { public prob120() { } static BigInteger findRMax(int i, BigInteger asqr) { BigInteger max = BigInteger.ZERO; BigInteger last = BigInteger.ZERO; for (int n = 1; n < 5000; n++) { BigInteger p1 = new BigInteger("" + (i - 1)); BigInteger p2 = new BigInteger("" + (i + 1)); BigInteger x1 = (p1.pow(n)).add(p2.pow(n)); BigInteger r = x1.mod(asqr); if (r.compareTo(max) > 0) { max = r; } last = x1; n++; } return max; } public static void main(String [] args) { BigInteger sum = BigInteger.ZERO; for (int i = 3; i <= 1000; i++) { BigInteger bi = new BigInteger("" + i); BigInteger x2 = bi.multiply(bi); BigInteger max = findRMax(i, x2); sum = sum.add(max); if (i % (10) == 0) { System.out.println(i + ", " + sum); } } } }