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 May 21, 2026
Problem #0120
Level Level 03
Solved By 15,550
Languages C++, Python
Answer 333082500
Length 383 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

Mathematical Development

Binomial Expansion Modulo a2a^2

Using the binomial theorem:

(a−1)n=∑k=0n(nk)ak(−1)n−k(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 k≥2k \ge 2 are divisible by a2a^2):

(a−1)n+(a+1)n≡[(−1)n+1]+[(−1)n−1+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)n−1=−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)n−1=1(-1)^n = -1, \quad (-1)^{n-1} = 1 r=(−1+1)+(1+1)⋅na=2na mod a2r = (-1 + 1) + (1 + 1) \cdot na = 2na \bmod a^2

Maximizing rr for Odd nn

For odd nn, r=2na mod a2r = 2na \bmod a^2. We want to maximize 2na mod a22na \bmod a^2.

Let m=n mod am = n \bmod a (since 2na mod a22na \bmod a^2 depends only on n mod an \bmod a, as 2na mod a2=a(2n mod a)2na \bmod a^2 = a(2n \bmod a)… let me be more precise).

Actually: 2na mod a2=a⋅(2n mod a)2na \bmod a^2 = a \cdot (2n \bmod a). So we want to maximize 2n mod a2n \bmod a where nn is odd.

If aa is odd: As nn ranges over odd values, 2n mod a2n \bmod a takes all values in {0,1,…,a−1}\{0, 1, \ldots, a-1\} (since gcd⁡(2,a)=1\gcd(2, a) = 1). The maximum of 2n mod a2n \bmod a is a−1a - 1, giving rmax⁡=a(a−1)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., 2n≡2(mod4)2n \equiv 2 \pmod{4}. Then 2n mod a2n \bmod a can reach at most a−2a - 2 (when aa is even). So rmax⁡=a(a−2)r_{\max} = a(a-2).

Closed-Form Formula

rmax⁡(a)={a(a−1)if a is odda(a−2)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(a−1)+∑a=4a even1000a(a−2)\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 2n mod a2n \bmod a cycle through all residues {0,1,…,a−1}\{0, 1, \ldots, a-1\}. The maximum residue is a−1a - 1, achieved when 2n≡a−1(moda)2n \equiv a - 1 \pmod{a}, i.e., n≡(a−1)/2⋅1(moda)n \equiv (a-1)/2 \cdot 1 \pmod{a} (using the inverse of 2 mod aa). Since (a−1)/2(a-1)/2 is an integer and we can always find an odd nn achieving this, rmax⁡=a(a−1)r_{\max} = a(a-1).

For even aa: Write a=2ma = 2m. Then 2n mod a=2n mod 2m=2(n mod m)2n \bmod a = 2n \bmod 2m = 2(n \bmod m). For odd nn, n mod mn \bmod m ranges over values achievable by odd nn, and the maximum of 2(n mod m)2(n \bmod m) is 2(m−1)=a−22(m - 1) = a - 2. Thus rmax⁡=a(a−2)r_{\max} = a(a - 2).

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

Editorial

The decisive observation is that almost the entire binomial expansion disappears modulo a2a^2. After expanding (a−1)n(a-1)^n and (a+1)n(a+1)^n, only the constant term and the linear term in aa survive, so the remainder can be written down explicitly from the parity of nn. Even exponents contribute only the trivial remainder 22, so the maximum must come from odd exponents.

For odd nn, the remainder is 2na mod a22na \bmod a^2, which is the same as a⋅(2n mod a)a \cdot (2n \bmod a). That reduces the problem to a simple residue maximization. When aa is odd, the multiplier 2 is invertible modulo aa, so the largest attainable residue is a−1a-1; when aa is even, the best possible residue is a−2a-2. The implementation therefore evaluates the resulting closed form for each aa from 3 to 1000 and adds the values directly.

Pseudocode

Set the total to zero.

For each a from 3 through 1000:
    If a is odd, use r_max = a(a - 1).
    If a is even, use r_max = a(a - 2).
    Add r_max to the total.

Return the total.

Complexity Analysis

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

Answer

333082500\boxed{333082500}

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;
}