All Euler problems
Project Euler

Shifted Multiples

For a positive integer n with leading digit d_1 and remaining digits forming number m, define shift(n) = 10m + d_1 (cyclic left shift of digits). Let r(n) = n mod shift(n). Find S(N) = sum_(n=1)^N...

Source sync Apr 19, 2026
Problem #0805
Level Level 35
Solved By 217
Languages C++, Python
Answer 119719335
Length 188 words
modular_arithmeticbrute_forcesearch

Problem Statement

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

For a positive integer $n$, let $s(n)$ be the integer obtained be shifting the leftmost digit of the decimal representation of $n$ to the rightmost position. For example, $s(142857) = 142864$ and $s(10) = 1$.

For a positive rational number $r$, we define $N(r)$ as the smallest positive integer $n$ such that $s(n) = r \cdot n$.

If no such integer exists, then $N(r)$ is defined as zero.

For example, $N(3) = 142857$, $N\left(\frac{1}{10}\right) = 10$ and $n(2) = 0$.

Let $T(M)$ be the sum of $N(u^3/v^3)$ where $(u, v)$ ranges over all ordered pairs of coprime positive integes not exeeding $M$.

For example, $T(3) \equiv 26242973 \pmod{10^9 + 7}$.

Find $T(200)$. Give your answer modulo $10^9 + 7$.

Problem 805: Shifted Multiples

Mathematical Analysis

For a kk-digit number nn with leading digit dd and tail mm: n=d10k1+mn = d \cdot 10^{k-1} + m and shift(n)=10m+d\text{shift}(n) = 10m + d.

The remainder r(n)=nmod(10m+d)r(n) = n \bmod (10m+d). We can express nn in terms of shift(n)\text{shift}(n):

n=d10k1+m=d10k19+(10m+d)10k119()n = d \cdot 10^{k-1} + m = d \cdot \frac{10^k - 1}{9} + (10m+d) \cdot \frac{10^{k-1}-1}{9} \cdot (\ldots)

Derivation

Let s=10m+d=shift(n)s = 10m + d = \text{shift}(n). Then m=(sd)/10m = (s - d)/10 and n=d10k1+(sd)/10n = d \cdot 10^{k-1} + (s-d)/10.

So n=d(10k11/10)+s/10=d(10k1)/10+s/10n = d(10^{k-1} - 1/10) + s/10 = d(10^k - 1)/10 + s/10. Thus 10n=d(10k1)+s10n = d(10^k - 1) + s, giving 10ns=d(10k1)10n - s = d(10^k - 1), i.e., 10ns(mod10k1)10n \equiv s \pmod{10^k - 1}.

We can write nmodsn \bmod s directly by computing the quotient q=n/sq = \lfloor n/s \rfloor and r=nqsr = n - qs.

For efficient summation, we group by (k,d)(k, d) — digit length and leading digit — and sum over all valid mm values.

Proof of Correctness

The digit shift is well-defined for all multi-digit numbers. For single-digit numbers, shift(n)=n\text{shift}(n) = n, so r(n)=0r(n) = 0. The grouping by digit length ensures complete coverage without overlap.

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

Complexity Analysis

Direct computation: O(N)O(N). With grouping by digit length: O(9kmax10kmax)O(9 \cdot k_{\max} \cdot \sqrt{10^{k_{\max}}}) which can be sub-linear for structured summation.

Answer

119719335\boxed{119719335}

Code

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

C++ project_euler/problem_805/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    long long N = 10000, total = 0;
    for (long long n = 10; n <= N; n++) {
        string s = to_string(n);
        string shifted_s = s.substr(1) + s[0];
        long long shifted = stoll(shifted_s);
        if (shifted > 0) total += n % shifted;
    }
    cout << total << endl;
    return 0;
}