Project Euler Lab - Problem 338

#338 - Cutting Rectangular Grid Paper

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

A rectangular sheet of grid paper with integer dimensions \(w \times h\) is given. Its grid spacing is \(1\).
When we cut the sheet along the grid lines into two pieces and rearrange those pieces without overlap, we can make new rectangles with different dimensions.

For example, from a sheet with dimensions \(9 \times 4\), we can make rectangles with dimensions \(18 \times 2\), \(12 \times 3\) and \(6 \times 6\) by cutting and rearranging as below:

0338_gridpaper.gif

Similarly, from a sheet with dimensions \(9 \times 8\), we can make rectangles with dimensions \(18 \times 4\) and \(12 \times 6\).

For a pair \(w\) and \(h\), let \(F(w, h)\) be the number of distinct rectangles that can be made from a sheet with dimensions \(w \times h\).
For example, \(F(2,1) = 0\), \(F(2,2) = 1\), \(F(9,4) = 3\) and \(F(9,8) = 2\).
Note that rectangles congruent to the initial one are not counted in \(F(w, h)\).
Note also that rectangles with dimensions \(w \times h\) and dimensions \(h \times w\) are not considered distinct.

For an integer \(N\), let \(G(N)\) be the sum of \(F(w, h)\) for all pairs \(w\) and \(h\) which satisfy \(0 \lt h \le w \le N\).
We can verify that \(G(10) = 55\), \(G(10^3) = 971745\) and \(G(10^5) = 9992617687\).

Find \(G(10^{12})\). Give your answer modulo \(10^8\).

This problem is taken from Project Euler, Problem 338.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=338. Published Sunday, 15th May 2011, 01:00 am. Solved by 414 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.