Project Euler Lab - Problem 93

#93 - Arithmetic Expressions

● AppliedOfficial difficulty: 14%Optimal substructureTier B - browser, with the efficient algorithmNot viewed
↖ Euler Lab

By using each of the digits from the set, \(\{1, 2, 3, 4\}\), exactly once, and making use of the four arithmetic operations (\(+, -, \times, /\)) and brackets/parentheses, it is possible to form different positive integer targets.

For example,

\[\begin{align} 8 &= (4 \times (1 + 3)) / 2\\ 14 &= 4 \times (3 + 1 / 2)\\ 19 &= 4 \times (2 + 3) - 1\\ 36 &= 3 \times 4 \times (2 + 1) \end{align}\]

Note that concatenations of the digits, like \(12 + 34\), are not allowed.

Using the set, \(\{1, 2, 3, 4\}\), it is possible to obtain thirty-one different target numbers of which \(36\) is the maximum, and each of the numbers \(1\) to \(28\) can be obtained before encountering the first non-expressible number.

Find the set of four distinct digits, \(a \lt b \lt c \lt d\), for which the longest set of consecutive positive integers, \(1\) to \(n\), can be obtained, giving your answer as a string: abcd.

This problem is taken from Project Euler, Problem 93.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=93. Published Friday, 15th April 2005, 06:00 pm. Solved by 13,938 members at time of mirroring.

Why this is useful

Algorithmic Development. Optimal substructure and state-space reasoning are exactly how American-option pricing and optimal execution are solved (Phases 13, 16).

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

Prerequisites

Lessons that prepare you:
17.1 Python for Quants: NumPy, pandas, and Vectorization · 19.7 Dynamic Programming: Memoization and Tabulation · 19.6 Recurrence Relations and Generating Functions

Recommended stepping-stone problems: #74

Concepts: dynamic-programming string-processing

Likely techniques: hashing

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 B - browser, with the efficient algorithm
Runs in the browser only with the intended efficient algorithm; a naive loop will hit the timeout.

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.