All Euler problems
Project Euler

Square Remainders

Let r be the remainder when (a-1)^n + (a+1)^n is divided by a^2. For 3 <= a <= 1000, find sum r_max, where r_max is the maximum value of r over all positive integers n.

Source sync Apr 19, 2026
Problem #0120
Level Level 03
Solved By 15,550
Languages C++, Python
Answer 333082500
Length 267 words
modular_arithmeticcombinatoricsnumber_theory

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Let $r$ be the remainder when $(a - 1)^n + (a + 1)^n$ is divided by $a^2$.

For example, if $a = 7$ and $n = 3$, then $r = 42$: $6^3 + 8^3 = 728 \equiv 42 \mod 49$. And as $n$ varies, so too will $r$, but for $a = 7$ it turns out that $r_{\mathrm{max}} = 42$.

For $3 \le a \le 1000$, find $\displaystyle \sum r_{\mathrm{max}}$.

Problem 120: Square Remainders

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Answer

333082500\boxed{333082500}

Mathematical Analysis

Binomial Expansion Modulo a2a^2

Using the binomial theorem:

(a1)n=k=0n(nk)ak(1)nk(a-1)^n = \sum_{k=0}^{n} \binom{n}{k} a^k (-1)^{n-k} (a+1)n=k=0n(nk)ak(a+1)^n = \sum_{k=0}^{n} \binom{n}{k} a^k

When we add these and reduce modulo a2a^2, only terms with k=0k = 0 and k=1k = 1 survive (terms with k2k \ge 2 are divisible by a2a^2):

(a1)n+(a+1)n[(1)n+1]+[(1)n1+1]na(moda2)(a-1)^n + (a+1)^n \equiv [(-1)^n + 1] + [(-1)^{n-1} + 1] \cdot na \pmod{a^2}

Case Analysis

When nn is even:

(1)n=1,(1)n1=1(-1)^n = 1, \quad (-1)^{n-1} = -1 r=(1+1)+(1+1)na=2r = (1 + 1) + (-1 + 1) \cdot na = 2

When nn is odd:

(1)n=1,(1)n1=1(-1)^n = -1, \quad (-1)^{n-1} = 1 r=(1+1)+(1+1)na=2namoda2r = (-1 + 1) + (1 + 1) \cdot na = 2na \bmod a^2

Maximizing rr for Odd nn

For odd nn, r=2namoda2r = 2na \bmod a^2. We want to maximize 2namoda22na \bmod a^2.

Let m=nmodam = n \bmod a (since 2namoda22na \bmod a^2 depends only on nmodan \bmod a, as 2namoda2=a(2nmoda)2na \bmod a^2 = a(2n \bmod a)… let me be more precise).

Actually: 2namoda2=a(2nmoda)2na \bmod a^2 = a \cdot (2n \bmod a). So we want to maximize 2nmoda2n \bmod a where nn is odd.

If aa is odd: As nn ranges over odd values, 2nmoda2n \bmod a takes all values in {0,1,,a1}\{0, 1, \ldots, a-1\} (since gcd(2,a)=1\gcd(2, a) = 1). The maximum of 2nmoda2n \bmod a is a1a - 1, giving rmax=a(a1)r_{\max} = a(a-1).

If aa is even: As nn ranges over odd values, 2n2n ranges over {2,6,10,}\{2, 6, 10, \ldots\}, i.e., 2n2(mod4)2n \equiv 2 \pmod{4}. Then 2nmoda2n \bmod a can reach at most a2a - 2 (when aa is even). So rmax=a(a2)r_{\max} = a(a-2).

Closed-Form Formula

rmax(a)={a(a1)if a is odda(a2)if a is evenr_{\max}(a) = \begin{cases} a(a-1) & \text{if } a \text{ is odd} \\ a(a-2) & \text{if } a \text{ is even} \end{cases}

Summation

a=31000rmax(a)=a=3a odd999a(a1)+a=4a even1000a(a2)\sum_{a=3}^{1000} r_{\max}(a) = \sum_{\substack{a=3 \\ a \text{ odd}}}^{999} a(a-1) + \sum_{\substack{a=4 \\ a \text{ even}}}^{1000} a(a-2)

Computing this sum yields 333082500.

Proof of the Formula

For odd aa: Since gcd(2,a)=1\gcd(2, a) = 1, as nn ranges over all odd positive integers, the residues 2nmoda2n \bmod a cycle through all residues {0,1,,a1}\{0, 1, \ldots, a-1\}. The maximum residue is a1a - 1, achieved when 2na1(moda)2n \equiv a - 1 \pmod{a}, i.e., n(a1)/21(moda)n \equiv (a-1)/2 \cdot 1 \pmod{a} (using the inverse of 2 mod aa). Since (a1)/2(a-1)/2 is an integer and we can always find an odd nn achieving this, rmax=a(a1)r_{\max} = a(a-1).

For even aa: Write a=2ma = 2m. Then 2nmoda=2nmod2m=2(nmodm)2n \bmod a = 2n \bmod 2m = 2(n \bmod m). For odd nn, nmodmn \bmod m ranges over values achievable by odd nn, and the maximum of 2(nmodm)2(n \bmod m) is 2(m1)=a22(m - 1) = a - 2. Thus rmax=a(a2)r_{\max} = a(a - 2).

Complexity

  • Time: O(N)O(N) where N=1000N = 1000.
  • Space: O(1)O(1).

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_120/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    long long total = 0;
    for (int a = 3; a <= 1000; a++) {
        if (a % 2 == 1) {
            total += (long long)a * (a - 1);
        } else {
            total += (long long)a * (a - 2);
        }
    }
    cout << total << endl;
    return 0;
}