Project Euler Lab - Problem 359

#359 - Hilbert's New Hotel

● AppliedOfficial difficulty: 23%PointsTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

An infinite number of people (numbered \(1\), \(2\), \(3\), etc.) are lined up to get a room at Hilbert's newest infinite hotel. The hotel contains an infinite number of floors (numbered \(1\), \(2\), \(3\), etc.), and each floor contains an infinite number of rooms (numbered \(1\), \(2\), \(3\), etc.).

Initially the hotel is empty. Hilbert declares a rule on how the \(n\)th person is assigned a room: person \(n\) gets the first vacant room in the lowest numbered floor satisfying either of the following:

  • the floor is empty
  • the floor is not empty, and if the latest person taking a room in that floor is person \(m\), then \(m + n\) is a perfect square

Person \(1\) gets room \(1\) in floor \(1\) since floor \(1\) is empty.
Person \(2\) does not get room \(2\) in floor \(1\) since \(1 + 2 = 3\) is not a perfect square.
Person \(2\) instead gets room \(1\) in floor \(2\) since floor \(2\) is empty.
Person \(3\) gets room \(2\) in floor \(1\) since \(1 + 3 = 4\) is a perfect square.

Eventually, every person in the line gets a room in the hotel.

Define \(P(f, r)\) to be \(n\) if person \(n\) occupies room \(r\) in floor \(f\), and \(0\) if no person occupies the room. Here are a few examples:
\(P(1, 1) = 1\)
\(P(1, 2) = 3\)
\(P(2, 1) = 2\)
\(P(10, 20) = 440\)
\(P(25, 75) = 4863\)
\(P(99, 100) = 19454\)

Find the sum of all \(P(f, r)\) for all positive \(f\) and \(r\) such that \(f \times r = 71328803586048\) and give the last \(8\) digits as your answer.

This problem is taken from Project Euler, Problem 359.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=359. Published Saturday, 19th November 2011, 10:00 pm. Solved by 1,779 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.

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.