Project Euler Lab - Problem 494

#494 - Collatz Prefix Families

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

The Collatz sequence is defined as: \(a_{i+1} = \left\{ \large{\frac {a_i} 2 \atop 3 a_i+1} {\text{if }a_i\text{ is even} \atop \text{if }a_i\text{ is odd}} \right.\).

The Collatz conjecture states that starting from any positive integer, the sequence eventually reaches the cycle \(1,4,2,1, \dots\).
We shall define the sequence prefix \(p(n)\) for the Collatz sequence starting with \(a_1 = n\) as the sub-sequence of all numbers not a power of \(2\) (\(2^0=1\) is considered a power of \(2\) for this problem). For example:
\(p(13) = \{13, 40, 20, 10, 5\}\)
\(p(8) = \{\}\)
Any number invalidating the conjecture would have an infinite length sequence prefix.

Let \(S_m\) be the set of all sequence prefixes of length \(m\). Two sequences \(\{a_1, a_2, \dots, a_m\}\) and \(\{b_1, b_2, \dots, b_m\}\) in \(S_m\) are said to belong to the same prefix family if \(a_i \lt a_j\) if and only if \(b_i \lt b_j\) for all \(1 \le i,j \le m\).

For example, in \(S_4\), \(\{6, 3, 10, 5\}\) is in the same family as \(\{454, 227, 682, 341\}\), but not \(\{113, 340, 170, 85\}\).
Let \(f(m)\) be the number of distinct prefix families in \(S_m\).
You are given \(f(5) = 5\), \(f(10) = 55\), \(f(20) = 6771\).

Find \(f(90)\).

This problem is taken from Project Euler, Problem 494.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=494. Published Saturday, 20th December 2014, 07:00 pm. Solved by 269 members at time of mirroring.

Why this is useful

Algorithmic Development. Graph/state-space search transfers to routing, dependency resolution, and execution-path optimisation (Phases 16, 17).

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.