Project Euler Lab - Problem 287

#287 - Quadtree Encoding (a Simple Compression Algorithm)

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

The quadtree encoding allows us to describe a \(2^N \times 2^N\) black and white image as a sequence of bits (0 and 1). Those sequences are to be read from left to right like this:

  • the first bit deals with the complete \(2^N \times 2^N\) region;
  • "0" denotes a split:
    the current \(2^n \times 2^n\) region is divided into \(4\) sub-regions of dimension \(2^{n - 1} \times 2^{n - 1}\),
    the next bits contains the description of the top left, top right, bottom left and bottom right sub-regions - in that order;
  • "10" indicates that the current region contains only black pixels;
  • "11" indicates that the current region contains only white pixels.

Consider the following \(4 \times 4\) image (colored marks denote places where a split can occur):

0287_quadtree.gif

This image can be described by several sequences, for example : "001010101001011111011010101010", of length \(30\), or
"0100101111101110", of length \(16\), which is the minimal sequence for this image.

For a positive integer \(N\), define \(D_N\) as the \(2^N \times 2^N\) image with the following coloring scheme:

  • the pixel with coordinates \(x = 0, y = 0\) corresponds to the bottom left pixel,
  • if \((x - 2^{N - 1})^2 + (y - 2^{N - 1})^2 \le 2^{2N - 2}\) then the pixel is black,
  • otherwise the pixel is white.

What is the length of the minimal sequence describing \(D_{24}\)?

This problem is taken from Project Euler, Problem 287.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=287. Published Saturday, 10th April 2010, 09:00 am. Solved by 1,704 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.