Project Euler Lab - Problem 980

#980 - The Quaternion Group I

● AppliedOfficial difficulty: 24%WordsTier B - browser, with the efficient algorithmNot viewed
↖ Euler Lab

Starting from an empty string, we want to build a string with letters "x", "y", "z". At each step, one of the following operations is performed:

  • insert two consecutive identical letters "xx", "yy" or "zz" anywhere into the string;
  • replace one letter in the string with two consecutive letters, according to the rule: "x" \(\to\) "yz", "y" \(\to\) "zx", "z" \(\to\) "xy";
  • exchange two consecutive different letters in the string, e.g. "xy" \(\to\) "yx", "zx" \(\to\) "xz", etc.

A string is called neutral if it is possible to produce the string from the empty string after an even number of steps.

We define a sequence \((a_n)_{n \ge 0}\): \(a_0=88\,888\,888\) and \(a_n=(8888\cdot a_{n-1})\bmod 888\,888\,883\) for \(n \gt 0\).

Let \(b_n = a_n \bmod 3\). For each \(i \ge 0\), a string \(c(i)\) of length \(50\) is defined by translating the finite sequence \(b_{50i},b_{50i+1},\dots,b_{50i+49}\) via the rule: \(0 \to\) "x", \(1 \to\) "y", \(2 \to\) "z".

Let \(F(N)\) be the number of ordered pairs \((i, j)\) with \(0 \le i, j \lt N\) such that the concatenated string \(c(i)c(j)\) is neutral.
For example, \(F(10) = 13\) and \(F(100) = 1224\).

Find \(F(10^6)\).

This problem is taken from Project Euler, Problem 980.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=980. Published Sunday, 25th January 2026, 10:00 am. Solved by 381 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 · 2.7 Sequences, Series, Convergence, and Power Series

Recommended stepping-stone problems: #98 · #191 · #346

Concepts: sequences-series string-processing

Likely techniques: memoization

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.