### Square remainders题号：120 难度： 25 中英对照

Let r be the remainder when (a−1)n + (a+1)n is divided by a2.

For example, if a = 7 and n = 3, then r = 42: 63 + 83 = 728 ≡ 42 mod 49. And as n varies, so too will r, but for a = 7 it turns out that rmax = 42.

For 3 ≤ a ≤ 1000, find rmax.

### Code

using System;
namespace euler
{
class Problem120
{
public static void Main(string[] args)
{
var start = DateTime.Now;
var result = Run();
var end = DateTime.Now;
Console.WriteLine(result);
Console.WriteLine("{0} ms", (end - start).TotalMilliseconds);
}
public static long Run()
{
int sum = 0;
for (int a = 3; a <= 1000; a = a + 2)
sum = sum + a * (a - 1);
for (int a = 4; a <= 1000; a = a + 2)
sum = sum + a * (a - 2);
return sum;
}
}
}
333082500
1 ms
public class p100 {
public static void main(String[] args) {
long start=System.nanoTime();
long result = run();
long end=System.nanoTime();
System.out.println(result);
System.out.println( (end-start)/1000000 + "ms" );
}
public static long run() {
int sum = 0;
for (int a = 3; a <= 1000; a = a + 2)
sum = sum + a * (a - 1);
for (int a = 4; a <= 1000; a = a + 2)
sum = sum + a * (a - 2);
return sum;
}
}
333082500
0ms