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.
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 be a -digit number () with last digit . Write where . The rotated number is . The condition for a positive integer is equivalent to:
Proof. From :
Lemma 1 (Bounds on ). The multiplier satisfies .
Proof. Since and are both -digit numbers (or could have fewer digits if , but leads to which has digits while , a contradiction for ), we need . Then and , so . Also since and actually requires . So .
Theorem 2 (Digit-by-digit cyclic construction). For fixed and last digit , the digits of satisfy a carry-propagation recurrence. Starting from the rightmost digit , each preceding digit is determined by:
where is the carry from position . The sequence of digits is eventually periodic since the carry is bounded.
Proof. The condition in terms of individual digits: multiplying by must produce . Column-by-column multiplication from right to left:
- Position :
- Position ():
- Position : (with final carry 0 and result digit )
Since each and (because , so ), 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 produces ), we get a valid of length equal to the cycle length.
Lemma 2. The last digit must satisfy , and for each pair, valid numbers exist for all cycle lengths that are multiples of the fundamental period of the carry recurrence, up to (since ).
Proof. As shown in Lemma 1, is impossible. For each , the carry recurrence starting from , generates digits with carries . A valid of length requires returning to carry with the final digit being after steps. This happens at all multiples of the fundamental cycle length.
Editorial
The solver works digit by digit from the rightmost end. Once the multiplier and the final digit are fixed, the multiplication relation determines the next digit and carry uniquely. So each pair 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 , 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 and , and extending each chain up to 100 digits, covers every admissible number below .
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: arithmetic operations. Effectively .
- Space: for storing digits of the current candidate.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
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), generate digits
right-to-left using the recurrence from the multiplication condition.
"""
def solve():
MOD = 100000
total = 0
for k in range(1, 10):
for r in range(1, 10):
digit = r
carry = 0
last5 = 0
pow10 = 1 # 10^step mod MOD
for step in range(100):
# Place current digit at position 'step' (from right)
if step < 5:
last5 = (last5 + digit * pow10) % MOD
pow10 = (pow10 * 10) % MOD
# Compute next digit and carry
val = k * digit + carry
next_digit = val % 10
next_carry = val // 10
d = step + 1 # number of digits so far
if d >= 2 and next_digit == r and next_carry == 0:
# Valid d-digit number; check leftmost digit nonzero
if digit != 0:
total = (total + last5) % MOD
digit = next_digit
carry = next_carry
print(total)
solve()