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 Foundation
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 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: 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()