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 Apr 19, 2026
Problem #0168
Level Level 08
Solved By 3,104
Languages C++, Python
Answer 59206
Length 366 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 Foundation

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

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

Proof. From m=knm = kn:

r10d1+q=k(10q+r)r \cdot 10^{d-1} + q = k(10q + r) r10d1kr=10kqqr \cdot 10^{d-1} - kr = 10kq - q r(10d1k)=q(10k1)r(10^{d-1} - k) = q(10k - 1) q=r(10d1k)10k1q = \frac{r(10^{d-1} - k)}{10k - 1}

\square

Lemma 1 (Bounds on kk). The multiplier kk satisfies 1k91 \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 d1d-1 digits while knn10d1kn \geq n \geq 10^{d-1}, a contradiction for d2d \geq 2), we need r1r \geq 1. Then m<10dm < 10^d and n10d1n \geq 10^{d-1}, so k=m/n<10d/10d1=10k = m/n < 10^d / 10^{d-1} = 10. Also k1k \geq 1 since m10d1n/10m \geq 10^{d-1} \geq n/10 and actually m=knnm = kn \geq n requires k1k \geq 1. So 1k91 \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 ad1,ad2,a_{d-1}, a_{d-2}, \ldots is determined by:

kai+ci=10ci1+ai1k \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=a1a2adn = a_1 a_2 \cdots a_d by kk must produce m=ada1a2ad1m = a_d a_1 a_2 \cdots a_{d-1}. Column-by-column multiplication from right to left:

  • Position dd: kad=10cd1+ad1k \cdot a_d = 10c_{d-1} + a_{d-1}
  • Position ii (2id12 \leq i \leq d-1): kai+ci=10ci1+ai1k \cdot a_i + c_i = 10c_{i-1} + a_{i-1}
  • Position 11: ka1+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 k9+8=89<90k \cdot 9 + 8 = 89 < 90, so c8c \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 r1r \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 ad1,ad2,a_{d-1}, a_{d-2}, \ldots with carries cd1,cd2,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 actual implementation tracks the carry recurrence carefully and checks the cycle-closing condition at each length.). We compute next digit and carry. We then check cycle closure: need carry = 0 and new_digit produces r. Finally, i.e., k * new_digit + carry_at_this_step should close.

Pseudocode

Start: digit = r, carry = 0
Compute next digit and carry
Check cycle closure: need carry = 0 and new_digit produces r
i.e., k * new_digit + carry_at_this_step should close
Actually: check if carry = 0 and the leading digit gives
k * a_1 + c_1 = r (the rotation condition)
Actually: need k*a_1 + c_1 = a_d = r with no further carry
This means the cycle has closed
but must verify the leading digit a_1 != 0
(no leading zeros in n)
Simpler: just check closing condition properly

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