Project Euler Lab - Problem 963

#963 - Removing Trits

● ResearchOfficial difficulty: 100%Two-player gamesTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

NOTE: This problem is related to Problem 882. It is recommended to solve that problem before doing this one.

Two players are playing a game. When the game starts, each player holds a paper with two positive integers written on it.
They make moves in turn. At a player's turn, the player can do one of the following:

  • pick a number on the player's own paper and change it by removing a \(0\) from its ternary expansionbase-\(3\) expansion;
  • pick a number on the opponent's paper and change it by removing a \(1\) from its ternary expansion;
  • pick a number on either paper and change it by removing a \(2\) from its ternary expansion.

The player that is unable to make a move loses.
Leading zeros are not allowed in any ternary expansion; in particular nobody can make a move on the number \(0\).

An initial setting is called fair if whichever player moves first will lose the game if both play optimally.

For example, if initially the integers on the paper of the first player are \(1, 5\) and those on the paper of the second player are \(2, 4\), then this is a fair initial setting, which we can denote as \((1, 5 \mid 2, 4)\).
Note that the order of the two integers on a paper does not matter, but the order of the two papers matter.
Thus \((5, 1 \mid 4, 2)\) is considered the same as \((1, 5 \mid 2, 4)\), while \((2, 4 \mid 1, 5)\) is a different initial setting.

Let \(F(N)\) be the number of fair initial settings where each initial number does not exceed \(N\).
For example, \(F(5) = 21\).

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

This problem is taken from Project Euler, Problem 963.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=963. Published Sunday, 5th October 2025, 11:00 am. Solved by 74 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

Recommended stepping-stone problems: #331 · #391 · #953

Concepts: game-theory brute-force-reduction

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.