Project Euler Lab - Problem 879

#879 - Touch-screen Password

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

A touch-screen device can be unlocked with a "password" consisting of a sequence of two or more distinct spots that the user selects from a rectangular grid of spots on the screen. The user enters their sequence by touching the first spot, then tracing a straight line segment to the next spot, and so on until the end of the sequence. The user's finger remains in contact with the screen throughout, and may only move in straight line segments from spot to spot.

If the finger traces a straight line that passes over an intermediate spot, then that is treated as two line segments with the intermediate spot included in the password sequence. For example, on a \(3\times 3\) grid labelled with digits \(1\) to \(9\) (shown below), tracing \(1-9\) is interpreted as \(1-5-9\).

Once a spot has been selected it disappears from the screen. Thereafter, the spot may not be used as an endpoint of future line segments, and it is ignored by any future line segments which happen to pass through it. For example, tracing \(1-9-3-7\) (which crosses the \(5\) spot twice) will give the password \(1-5-9-6-3-7\).

1-5-9-6-3-7 example

There are \(389488\) different passwords that can be formed on a \(3 \times 3\) grid.

Find the number of different passwords that can be formed on a \(4 \times 4\) grid.

This problem is taken from Project Euler, Problem 879.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=879. Published Saturday, 24th February 2024, 04:00 pm. Solved by 535 members at time of mirroring.

Why this is useful

General Problem Solving. Builds computational thinking, decomposition, and debugging discipline - transferable, without a specific financial application.

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.6 Recurrence Relations and Generating Functions · 2.7 Sequences, Series, Convergence, and Power Series

Recommended stepping-stone problems: #119 · #918 · #506

Concepts: sequences-series

Likely techniques: hashing

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.