Project Euler Lab - Problem 419

#419 - Look and Say Sequence

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

The look and say sequence goes 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, ...
The sequence starts with 1 and all other members are obtained by describing the previous member in terms of consecutive digits.
It helps to do this out loud:
1 is 'one one' → 11
11 is 'two ones' → 21
21 is 'one two and one one' → 1211
1211 is 'one one, one two and two ones' → 111221
111221 is 'three ones, two twos and one one' → 312211
...

Define \(A(n)\), \(B(n)\) and \(C(n)\) as the number of ones, twos and threes in the \(n\)'th element of the sequence respectively.
One can verify that \(A(40) = 31254\), \(B(40) = 20259\) and \(C(40) = 11625\).

Find \(A(n)\), \(B(n)\) and \(C(n)\) for \(n = 10^{12}\).
Give your answer modulo \(2^{30}\) and separate your values for \(A\), \(B\) and \(C\) by a comma.
E.g. for \(n = 40\) the answer would be 31254,20259,11625

This problem is taken from Project Euler, Problem 419.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=419. Published Saturday, 16th March 2013, 10:00 pm. Solved by 574 members at time of mirroring.

Why this is useful

Mathematical Foundation. Exact integer reasoning and algorithmic efficiency. Foundational rigour and computational discipline rather than a direct trading application.

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.