Project Euler Lab - Problem 396

#396 - Weak Goodstein Sequence

● AdvancedOfficial difficulty: 55%RecurrencesTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

For any positive integer \(n\), the \(n\)th weak Goodstein sequence \(\{g_1, g_2, g_3, \dots\}\) is defined as:

  • \(g_1 = n\)
  • for \(k \gt 1\), \(g_k\) is obtained by writing \(g_{k-1}\) in base \(k\), interpreting it as a base \(k + 1\) number, and subtracting \(1\).
The sequence terminates when \(g_k\) becomes \(0\).

For example, the \(6\)th weak Goodstein sequence is \(\{6, 11, 17, 25, \dots\}\):

  • \(g_1 = 6\).
  • \(g_2 = 11\) since \(6 = 110_2\), \(110_3 = 12\), and \(12 - 1 = 11\).
  • \(g_3 = 17\) since \(11 = 102_3\), \(102_4 = 18\), and \(18 - 1 = 17\).
  • \(g_4 = 25\) since \(17 = 101_4\), \(101_5 = 26\), and \(26 - 1 = 25\).
and so on.

It can be shown that every weak Goodstein sequence terminates.

Let \(G(n)\) be the number of nonzero elements in the \(n\)th weak Goodstein sequence.
It can be verified that \(G(2) = 3\), \(G(4) = 21\) and \(G(6) = 381\).
It can also be verified that \(\sum G(n) = 2517\) for \(1 \le n \lt 8\).

Find the last \(9\) digits of \(\sum G(n)\) for \(1 \le n \lt 16\).

This problem is taken from Project Euler, Problem 396.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=396. Published Sunday, 30th September 2012, 02:00 am. Solved by 770 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.6 Recurrence Relations and Generating Functions · 2.7 Sequences, Series, Convergence, and Power Series

Recommended stepping-stone problems: #551 · #208 · #343

Concepts: sequences-series 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 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.