All Euler problems
Project Euler

Divisibility Multipliers

For each integer p > 1 coprime to 10, define the divisibility multiplier m(p) as the smallest positive integer m < p such that the function f(n) = floor(n/10) + (n mod 10) * m preserves divisibilit...

Source sync Apr 19, 2026
Problem #0274
Level Level 12
Solved By 1,644
Languages C++, Python
Answer 1601912348822
Length 284 words
modular_arithmeticnumber_theorybrute_force

Problem Statement

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

For each integer \(p > 1\) coprime to \(10\) there is a positive divisibility multiplier \(m < p\) which preserves divisibility by \(p\) for the following function on any positive integer, \(n\):

\(f(n) = (\text {all but the last digit of }n) + (\text {the last digit of }n) \cdot m\).

That is, if \(m\) is the divisibility multiplier for \(p\), then \(f(n)\) is divisible by \(p\) if and only if \(n\) is divisible by \(p\).

(When \(n\) is much larger than \(p\), \(f(n)\) will be less than \(n\) and repeated application of \(f\) provides a multiplicative divisibility test for \(p\).)

For example, the divisibility multiplier for \(113\) is \(34\).

\(f(76275) = 7627 + 5 \cdot 34 = 7797\): \(76275\) and \(7797\) are both divisible by \(113\).

\(f(12345) = 1234 + 5 \cdot 34 = 1404\): \(12345\) and \(1404\) are both not divisible by \(113\).

The sum of the divisibility multipliers for the primes that are coprime to \(10\) and less than \(1000\) is \(39517\). What is the sum of the divisibility multipliers for the primes that are coprime to \(10\) and less than \(10^7\)?

Problem 274: Divisibility Multipliers

Mathematical Foundation

Theorem 1 (Divisibility Multiplier is the Modular Inverse of 10). Let p>1p > 1 with gcd(p,10)=1\gcd(p, 10) = 1. Then m(p)=101(modp)m(p) = 10^{-1} \pmod{p}, i.e., the unique m{1,2,,p1}m \in \{1, 2, \ldots, p-1\} satisfying 10m1(modp)10m \equiv 1 \pmod{p}.

Proof. Write n=10q+rn = 10q + r where q=n/10q = \lfloor n/10 \rfloor and r=nmod10r = n \bmod 10. Then f(n)=q+rmf(n) = q + rm. Suppose pnp \mid n, i.e., 10q+r0(modp)10q + r \equiv 0 \pmod{p}. We require q+rm0(modp)q + rm \equiv 0 \pmod{p}.

From 10q+r010q + r \equiv 0, we get r10q(modp)r \equiv -10q \pmod{p}. Substituting:

q+rm=q+(10q)m=q(110m)0(modp).q + rm = q + (-10q)m = q(1 - 10m) \equiv 0 \pmod{p}.

For this to hold for all qq (since varying nn over multiples of pp produces all residues of qq modulo pp), we need 110m0(modp)1 - 10m \equiv 0 \pmod{p}, i.e., 10m1(modp)10m \equiv 1 \pmod{p}.

Conversely, if 10m1(modp)10m \equiv 1 \pmod{p}, then q(110m)0(modp)q(1 - 10m) \equiv 0 \pmod{p} for all qq, confirming that ff preserves divisibility. \square

Lemma 1 (Computation via Fermat’s Little Theorem). For prime p10p \nmid 10, m(p)=10p2modpm(p) = 10^{p-2} \bmod p.

Proof. By Fermat’s little theorem, 10p11(modp)10^{p-1} \equiv 1 \pmod{p}, so 1010p21(modp)10 \cdot 10^{p-2} \equiv 1 \pmod{p}, meaning 10p210^{p-2} is the multiplicative inverse of 1010 modulo pp. \square

Lemma 2 (Extended Euclidean Alternative). For any pp with gcd(p,10)=1\gcd(p, 10) = 1 (not necessarily prime), m(p)=101modpm(p) = 10^{-1} \bmod p can be computed in O(logp)O(\log p) time via the extended Euclidean algorithm.

Proof. The extended GCD computes s,ts, t with 10s+pt=110s + pt = 1, giving 101s(modp)10^{-1} \equiv s \pmod{p}. The algorithm runs in O(log(min(10,p)))=O(logp)O(\log(\min(10, p))) = O(\log p) steps. \square

Editorial

For each prime p coprime to 10, the divisibility multiplier m(p) is the smallest positive integer m < p such that for the operation f(n) = floor(n/10) + (n mod 10) * m, if p | n then p | f(n). This is equivalent to m(p) = 10^{-1} mod p (modular inverse of 10 mod p). Find the sum of m(p) for all primes p coprime to 10 with p < 10^7. We sieve of Eratosthenes. Finally, sum modular inverses.

Pseudocode

Sieve of Eratosthenes
Sum modular inverses
Compute 10^{-1} mod p via modular exponentiation

Complexity Analysis

  • Time: The sieve of Eratosthenes runs in O(NloglogN)O(N \log \log N). For each of the π(N)2\pi(N) - 2 primes (excluding 2 and 5), modular exponentiation costs O(logp)O(\log p). Total: O(NloglogN+π(N)logN)O(N \log \log N + \pi(N) \log N). By the prime number theorem, π(N)N/lnN\pi(N) \approx N / \ln N, so the second term is O(N)O(N). Overall: O(NloglogN)O(N \log \log N).
  • Space: O(N)O(N) for the sieve array.

Answer

1601912348822\boxed{1601912348822}

Code

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

C++ project_euler/problem_274/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

// Problem 274: Divisibility Multipliers
//
// For each prime p coprime to 10 (i.e., p > 5 and p != 2),
// the divisibility multiplier m(p) is the modular inverse of 10 mod p.
// Find sum of m(p) for all primes 5 < p < 10^7.

int main() {
    const int LIMIT = 10000000;

    // Sieve of Eratosthenes
    vector<bool> is_prime(LIMIT, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; (ll)i * i < LIMIT; i++) {
        if (is_prime[i]) {
            for (int j = i * i; j < LIMIT; j += i)
                is_prime[j] = false;
        }
    }

    ll total = 0;
    for (int p = 3; p < LIMIT; p++) {
        if (p == 5) continue;
        if (!is_prime[p]) continue;
        // Compute 10^{-1} mod p using Fermat's little theorem: 10^{p-2} mod p
        // Or use extended GCD
        ll inv = 1;
        ll base = 10 % p;
        ll exp = p - 2;
        ll mod = p;
        ll b = base;
        ll e = exp;
        ll r = 1;
        while (e > 0) {
            if (e & 1) r = r * b % mod;
            b = b * b % mod;
            e >>= 1;
        }
        total += r;
    }

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