All Euler problems
Project Euler

Solving the Equation 1/a + 1/b = p / 10^n

For each n from 1 to 9, count the number of solutions (a, b, p) where a, b, p are positive integers with a <= b, satisfying: (1)/(a) + (1)/(b) = (p)/(10^n) Sum the counts over n = 1, 2,..., 9.

Source sync Apr 19, 2026
Problem #0157
Level Level 08
Solved By 3,192
Languages C++, Python
Answer 53490
Length 250 words
number_theorylinear_algebramodular_arithmetic

Problem Statement

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

Consider the diophantine equation \(\frac 1 a + \frac 1 b = \frac p {10^n}\) with \(a, b, p, n\) positive integers and \(a \le b\).

For \(n=1\) this equation has \(20\) solutions that are listed below: \[ \begin {array}{ccccc} \frac {1}{1} + \frac {1}{1} = \frac {20}{10} & \frac {1}{1} + \frac {1}{2} = \frac {15}{10} & \frac {1}{1} + \frac {1}{5} = \frac {12}{10} & \frac {1}{1} + \frac {1}{10} = \frac {11}{10} & \frac {1}{2} + \frac {1}{2} = \frac {10}{10} \\[12pt] \frac {1}{2} + \frac {1}{5} = \frac {7}{10} & \frac {1}{2} + \frac {1}{10} = \frac {6}{10} & \frac {1}{3} + \frac {1}{6} = \frac {5}{10} & \frac {1}{3} + \frac {1}{15} = \frac {4}{10} & \frac {1}{4} + \frac {1}{4} = \frac {5}{10} \\[12pt] \frac {1}{4} + \frac {1}{20} = \frac {3}{10} & \frac {1}{5} + \frac {1}{5} = \frac {4}{10} & \frac {1}{5} + \frac {1}{10} = \frac {3}{10} & \frac {1}{6} + \frac {1}{30} = \frac {2}{10} & \frac {1}{10} + \frac {1}{10} = \frac {2}{10} \\[12pt] \frac {1}{11} + \frac {1}{110} = \frac {1}{10} & \frac {1}{12} + \frac {1}{60} = \frac {1}{10} & \frac {1}{14} + \frac {1}{35} = \frac {1}{10} & \frac {1}{15} + \frac {1}{30} = \frac {1}{10} & \frac {1}{20} + \frac {1}{20} = \frac {1}{10} \end {array} \] How many solutions has this equation for \(1 \le n \le 9\)?

Problem 157: Solving the Equation 1/a + 1/b = p / 10^n

Mathematical Analysis

Key Transformation

Let N=10nN = 10^n. From 1a+1b=pN\frac{1}{a} + \frac{1}{b} = \frac{p}{N}:

N(a+b)=pabN(a + b) = pab

Rearranging and completing the product:

pabNaNb=0pab - Na - Nb = 0

Multiply by pp and add N2N^2:

(paN)(pbN)=N2(pa - N)(pb - N) = N^2

Enumeration via Divisors

Let u=paNu = pa - N and v=pbNv = pb - N with uv=N2=102nuv = N^2 = 10^{2n}.

Claim: Both uu and vv must be positive.

Proof: Since a,b,p>0a, b, p > 0, we need pa>0pa > 0 and pb>0pb > 0. If u<0u < 0 and v<0v < 0, then u<N|u| < N and v<N|v| < N, giving uv<N2|u||v| < N^2, contradicting uv=N2uv = N^2. If one is positive and one negative, uv<0N2uv < 0 \ne N^2. So both are positive.

For each divisor uu of N2N^2 with 1uN1 \le u \le N (ensuring uvu \le v, which gives aba \le b):

  1. Set v=N2/uv = N^2 / u.
  2. Compute g=gcd(N+u,N+v)g = \gcd(N + u, N + v).
  3. Each divisor pp of gg yields a valid solution: a=(N+u)/pa = (N+u)/p, b=(N+v)/pb = (N+v)/p.
  4. Since aba \le b requires uvu \le v (already ensured), and a,ba, b are positive integers when pgcd(N+u,N+v)p | \gcd(N+u, N+v), the contribution from this divisor pair is τ(g)\tau(g), the number of positive divisors of gg.

Proof of Correctness

Given a divisor pair (u,v)(u, v) with uv=N2uv = N^2, and any positive divisor pp of gcd(N+u,N+v)\gcd(N+u, N+v):

  • a=(N+u)/pa = (N+u)/p and b=(N+v)/pb = (N+v)/p are positive integers.
  • (paN)(pbN)=uv=N2(pa - N)(pb - N) = uv = N^2, which expands to N(a+b)=pabN(a+b) = pab.
  • Hence 1a+1b=pN\frac{1}{a} + \frac{1}{b} = \frac{p}{N} with aba \le b.
  • Conversely, every solution (a,b,p)(a, b, p) arises this way.

Why divisors of 102n10^{2n} suffice

N2=102n=22n52nN^2 = 10^{2n} = 2^{2n} \cdot 5^{2n}. Its divisors are exactly the numbers 2i5j2^i \cdot 5^j with 0i,j2n0 \le i, j \le 2n. The number of divisors is (2n+1)2(2n+1)^2.

Counting formula

Answer=n=19u102n1u10nτ ⁣(gcd(10n+u,  10n+102n/u))\text{Answer} = \sum_{n=1}^{9} \sum_{\substack{u \mid 10^{2n} \\ 1 \le u \le 10^n}} \tau\!\left(\gcd(10^n + u,\; 10^n + 10^{2n}/u)\right)

Per-nn breakdown

nnCount
120
2102
3356
4958
52192
64456
78260
814088
923058

Complexity

The number of divisors of 102n10^{2n} is (2n+1)2(2n+1)^2. For each divisor pair, we compute a GCD (O(logN)O(\log N)) and count divisors (O(g)O(\sqrt{g})). Total work is negligible.

Answer

53490\boxed{53490}

Code

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

C++ project_euler/problem_157/solution.cpp
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

// Count number of positive divisors of n
int count_divisors(ll n) {
    if (n <= 0) return 0;
    int count = 0;
    for (ll d = 1; d * d <= n; d++) {
        if (n % d == 0) {
            count++;
            if (d != n / d) count++;
        }
    }
    return count;
}

// Get all divisors of 10^(2n) = 2^(2n) * 5^(2n)
vector<ll> get_divisors(int n2) {
    vector<ll> divs;
    for (int a = 0; a <= n2; a++) {
        for (int b = 0; b <= n2; b++) {
            ll d = 1;
            for (int i = 0; i < a; i++) d *= 2;
            for (int i = 0; i < b; i++) d *= 5;
            divs.push_back(d);
        }
    }
    sort(divs.begin(), divs.end());
    return divs;
}

int main() {
    ll total = 0;
    for (int n = 1; n <= 9; n++) {
        ll N = 1;
        for (int i = 0; i < n; i++) N *= 10;
        ll N2 = N * N;
        vector<ll> divs = get_divisors(2 * n);
        for (ll u : divs) {
            if (u > N) continue; // u <= N ensures a <= b
            ll v = N2 / u;
            ll g = __gcd(N + u, N + v);
            total += count_divisors(g);
        }
    }
    cout << total << endl;
    return 0;
}