Project Euler Lab - Problem 676

#676 - Matching Digit Sums

● ResearchOfficial difficulty: 68%Naive enumeration is infeasible; requires a mathematical reductionTier D - conceptual / notebook executionNot viewed
↖ Euler Lab

Let \(d(i,b)\) be the digit sum of the number \(i\) in base \(b\). For example \(d(9,2)=2\), since \(9=1001_2\). When using different bases, the respective digit sums most of the time deviate from each other, for example \(d(9,4)=3 \ne d(9,2)\).

However, for some numbers \(i\) there will be a match, like \(d(17,4)=d(17,2)=2\). Let \( M(n,b_1,b_2)\) be the sum of all natural numbers \(i \le n\) for which \(d(i,b_1)=d(i,b_2)\). For example, \(M(10,8,2)=18\), \(M(100,8,2)=292\) and \(M(10^6,8,2)=19173952\).

Find \(\displaystyle \sum_{k=3}^6 \sum_{l=1}^{k-2}M(10^{16},2^k,2^l)\), giving the last \(16\) digits as the answer.

This problem is taken from Project Euler, Problem 676.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=676. Published Saturday, 22nd June 2019, 04:00 pm. Solved by 274 members at time of mirroring.

Why this is useful

Optimization. The transferable skill is replacing infeasible enumeration with a mathematical reduction - the core move in calibration and large-scale computation (Phases 10, 13).

We classify relevance honestly - not every Euler problem is a trading application.

Prerequisites

Lessons that prepare you:
19.14 Computational Complexity, Feasibility Estimation, and Proving Algorithms Correct · 19.7 Dynamic Programming: Memoization and Tabulation

Recommended stepping-stone problems: #358 · #412 · #634

Concepts: brute-force-reduction

Likely techniques: digit-dp

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier D - conceptual / notebook execution
Too heavy for browser Pyodide at original scale: the browser is used for planning, small cases and reasoning; full scale runs in a local notebook.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.