Project Euler Lab - Problem 637

#637 - Flexible Digit Sum

● AdvancedOfficial difficulty: 36%Naive enumeration is infeasible; requires a mathematical reductionTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Given any positive integer \(n\), we can construct a new integer by inserting plus signs between some of the digits of the base \(B\) representation of \(n\), and then carrying out the additions.

For example, from \(n=123_{10}\) (\(n\) in base \(10\)) we can construct the four base \(10\) integers \(123_{10}\), \(1+23=24_{10}\), \(12+3=15_{10}\) and \(1+2+3=6_{10}\).

Let \(f(n,B)\) be the smallest number of steps needed to arrive at a single-digit number in base \(B\). For example, \(f(7,10)=0\) and \(f(123,10)=1\).

Let \(g(n,B_1,B_2)\) be the sum of the positive integers \(i\) not exceeding \(n\) such that \(f(i,B_1)=f(i,B_2)\).

You are given \(g(100,10,3)=3302\).

Find \(g(10^7,10,3)\).

This problem is taken from Project Euler, Problem 637.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=637. Published Sunday, 23rd September 2018, 01:00 am. Solved by 394 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 · 19.10 Search: Backtracking, Branch-and-Bound, Binary Search, Meet-in-the-Middle

Recommended stepping-stone problems: #271 · #539 · #731

Concepts: brute-force-reduction

Likely techniques: binary-search 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 C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided 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.