All Euler problems
Project Euler

Number Rotations

Find all integers n with 10 <= n < 10^100 such that moving the last digit of n to the front produces a number that is an exact multiple of n. Sum the last 5 digits of all such n.

Source sync May 21, 2026
Problem #0168
Level Level 08
Solved By 3,104
Languages C++, Python
Answer 59206
Length 432 words
modular_arithmeticsequencealgebra

Problem Statement

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

Consider the number \(142857\). We can right-rotate this number by moving the last digit (\(7\)) to the front of it, giving us \(714285\).

It can be verified that \(714285 = 5 \times 142857\).

This demonstrates an unusual property of \(142857\): it is a divisor of its right-rotation.

Find the last \(5\) digits of the sum of all integers \(n\), \(10 < n < 10^{100}\), that have this property.

Problem 168: Number Rotations

Mathematical Development

Theorem 1 (Algebraic formulation). Let nn be a dd-digit number (d≥2d \geq 2) with last digit rr. Write n=10q+rn = 10q + r where q=⌊n/10⌋q = \lfloor n/10 \rfloor. The rotated number is m=r⋅10d−1+qm = r \cdot 10^{d-1} + q. The condition m=knm = kn for a positive integer kk is equivalent to:

q=r(10d−1−k)10k−1q = \frac{r(10^{d-1} - k)}{10k - 1}

Proof. From m=knm = kn:

r⋅10d−1+q=k(10q+r)r \cdot 10^{d-1} + q = k(10q + r) r⋅10d−1−kr=10kq−qr \cdot 10^{d-1} - kr = 10kq - q r(10d−1−k)=q(10k−1)r(10^{d-1} - k) = q(10k - 1) q=r(10d−1−k)10k−1q = \frac{r(10^{d-1} - k)}{10k - 1}

□\square

Lemma 1 (Bounds on kk). The multiplier kk satisfies 1≤k≤91 \leq k \leq 9.

Proof. Since mm and nn are both dd-digit numbers (or mm could have fewer digits if r=0r = 0, but r=0r = 0 leads to m=qm = q which has d−1d-1 digits while kn≥n≥10d−1kn \geq n \geq 10^{d-1}, a contradiction for d≥2d \geq 2), we need r≥1r \geq 1. Then m<10dm < 10^d and n≥10d−1n \geq 10^{d-1}, so k=m/n<10d/10d−1=10k = m/n < 10^d / 10^{d-1} = 10. Also k≥1k \geq 1 since m≥10d−1≥n/10m \geq 10^{d-1} \geq n/10 and actually m=kn≥nm = kn \geq n requires k≥1k \geq 1. So 1≤k≤91 \leq k \leq 9. □\square

Theorem 2 (Digit-by-digit cyclic construction). For fixed k∈{1,…,9}k \in \{1, \ldots, 9\} and last digit r∈{1,…,9}r \in \{1, \ldots, 9\}, the digits of nn satisfy a carry-propagation recurrence. Starting from the rightmost digit ad=ra_d = r, each preceding digit ad−1,ad−2,…a_{d-1}, a_{d-2}, \ldots is determined by:

k⋅ai+ci=10ci−1+ai−1k \cdot a_i + c_i = 10 c_{i-1} + a_{i-1}

where cic_i is the carry from position ii. The sequence of digits is eventually periodic since the carry ci∈{0,1,…,8}c_i \in \{0, 1, \ldots, 8\} is bounded.

Proof. The condition m=knm = kn in terms of individual digits: multiplying n=a1a2⋯adn = a_1 a_2 \cdots a_d by kk must produce m=ada1a2⋯ad−1m = a_d a_1 a_2 \cdots a_{d-1}. Column-by-column multiplication from right to left:

  • Position dd: k⋅ad=10cd−1+ad−1k \cdot a_d = 10c_{d-1} + a_{d-1}
  • Position ii (2≤i≤d−12 \leq i \leq d-1): k⋅ai+ci=10ci−1+ai−1k \cdot a_i + c_i = 10c_{i-1} + a_{i-1}
  • Position 11: k⋅a1+c1=adk \cdot a_1 + c_1 = a_d (with final carry 0 and result digit ada_d)

Since each ai∈{0,…,9}a_i \in \{0, \ldots, 9\} and ci∈{0,…,8}c_i \in \{0, \ldots, 8\} (because k⋅9+8=89<90k \cdot 9 + 8 = 89 < 90, so c≤8c \leq 8), the carry sequence determines the digit sequence. With 9 possible carry values, the sequence becomes periodic with period at most 9. For each valid cycle closing (where c1c_1 produces ad=ra_d = r), we get a valid nn of length equal to the cycle length. □\square

Lemma 2. The last digit rr must satisfy r≥1r \geq 1, and for each (k,r)(k, r) pair, valid numbers nn exist for all cycle lengths dd that are multiples of the fundamental period of the carry recurrence, up to d=99d = 99 (since n<10100n < 10^{100}).

Proof. As shown in Lemma 1, r=0r = 0 is impossible. For each (k,r)(k, r), the carry recurrence starting from cd=0c_d = 0, ad=ra_d = r generates digits ad−1,ad−2,…a_{d-1}, a_{d-2}, \ldots with carries cd−1,cd−2,…c_{d-1}, c_{d-2}, \ldots. A valid nn of length dd requires returning to carry c=0c = 0 with the final digit being ad=ra_d = r after dd steps. This happens at all multiples of the fundamental cycle length. □\square

Editorial

The solver works digit by digit from the rightmost end. Once the multiplier kk and the final digit rr are fixed, the multiplication relation determines the next digit and carry uniquely. So each pair (k,r)(k,r) generates a deterministic digit stream rather than a branching search tree.

As the digits are produced, the program keeps the last five digits of the current candidate modulo 100000100000, because that is all the final sum needs. Whenever the recurrence closes cleanly, meaning the carry vanishes and the left end reconnects to the original last digit, a valid rotated multiple has been found. Enumerating all k∈{1,…,9}k \in \{1,\dots,9\} and r∈{1,…,9}r \in \{1,\dots,9\}, and extending each chain up to 100 digits, covers every admissible number below 1010010^{100}.

Pseudocode

Initialize the answer modulo 100000 to zero.

For each multiplier $k$ from 1 to 9:
    For each possible last digit $r$ from 1 to 9:
        Start the recurrence with current digit $r$ and carry 0.
        Build the number from right to left for lengths up to 100 digits,
        updating the stored last-five-digit residue as new digits are appended.

        After each step, compute the next digit and next carry from the multiplication rule.
        Whenever the chain has length at least 2 and the new state closes back to digit $r$
        with zero carry, record the current residue as one valid number.

Return the final sum modulo 100000.

Complexity Analysis

  • Time: O(9×9×100)=O(8,100)O(9 \times 9 \times 100) = O(8{,}100) arithmetic operations. Effectively O(1)O(1).
  • Space: O(100)O(100) for storing digits of the current candidate.

Answer

59206\boxed{59206}

Code

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

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

/*
 * Problem 168: Number Rotations
 *
 * Find all n with 10 <= n < 10^100 such that rotating the last digit to
 * the front gives a multiple of n. Sum the last 5 digits of all such n.
 *
 * For each multiplier k (1..9) and last digit r (1..9), we generate digits
 * right-to-left: starting with digit r and carry 0, next digit = (k*prev_digit + carry) % 10,
 * new carry = (k*prev_digit + carry) / 10. After d steps, if we return to
 * (digit=r, carry=0), we have a valid d-digit number. Also need first digit != 0.
 */

int main(){
    const long long MOD = 100000;
    long long total = 0;

    for(int k = 1; k <= 9; k++){
        for(int r = 1; r <= 9; r++){
            // Generate digits right to left
            // digits[0] = rightmost digit = r (this is a_d)
            // digits[1] = a_{d-1}, etc.
            // digits[d-1] = a_1 (leftmost)
            // After d steps of generation, we need current digit = r and carry = 0
            // Also a_1 (= digits[d-1]) must be nonzero

            int digit = r;
            int carry = 0;
            // Store last 5 digits as we go (tracking powers of 10 mod 100000)
            // The digits are generated right to left: digit 0 is units, digit 1 is tens, etc.
            // last5 of n = sum of digit[i] * 10^i for i=0..4

            long long last5 = 0;
            long long pow10 = 1; // 10^i mod MOD

            for(int step = 0; step < 100; step++){
                // Current digit goes at position 'step' (from the right)
                if(step < 5){
                    last5 = (last5 + (long long)digit * pow10) % MOD;
                    pow10 = (pow10 * 10) % MOD;
                }

                // Check if we've completed a valid cycle (step+1 digits = d)
                // Need d >= 2, and after generating all d digits, the NEXT step
                // would produce (r, 0) again.
                // Actually: after placing digit at position 'step', compute next:
                int next_digit = (k * digit + carry) % 10;
                int next_carry = (k * digit + carry) / 10;

                int d = step + 1;
                if(d >= 2 && next_digit == r && next_carry == 0){
                    // Valid! The number has d digits.
                    // Check that the leftmost digit (digits[d-1] = current 'digit') is nonzero
                    if(digit != 0){
                        total = (total + last5) % MOD;
                    }
                }

                digit = next_digit;
                carry = next_carry;
            }
        }
    }

    cout << total << endl;
    return 0;
}