Project Euler Lab - Problem 387

#387 - Harshad Numbers

● AppliedOfficial difficulty: 14%DivisibilityTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

A Harshad or Niven number is a number that is divisible by the sum of its digits.
\(201\) is a Harshad number because it is divisible by \(3\) (the sum of its digits.)
When we truncate the last digit from \(201\), we get \(20\), which is a Harshad number.
When we truncate the last digit from \(20\), we get \(2\), which is also a Harshad number.
Let's call a Harshad number that, while recursively truncating the last digit, always results in a Harshad number a right truncatable Harshad number.

Also:
\(201/3=67\) which is prime.
Let's call a Harshad number that, when divided by the sum of its digits, results in a prime a strong Harshad number.

Now take the number \(2011\) which is prime.
When we truncate the last digit from it we get \(201\), a strong Harshad number that is also right truncatable.
Let's call such primes strong, right truncatable Harshad primes.

You are given that the sum of the strong, right truncatable Harshad primes less than \(10000\) is \(90619\).

Find the sum of the strong, right truncatable Harshad primes less than \(10^{14}\).

This problem is taken from Project Euler, Problem 387.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=387. Published Sunday, 3rd June 2012, 11:00 am. Solved by 5,359 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.

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.