Project Euler Lab - Problem 538

#538 - Maximum Quadrilaterals

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

Consider a positive integer sequence \(S = (s_1, s_2, \dots, s_n)\).

Let \(f(S)\) be the perimeter of the maximum-area quadrilateral whose side lengths are \(4\) elements \((s_i, s_j, s_k, s_l)\) of \(S\) (all \(i, j, k, l\) distinct). If there are many quadrilaterals with the same maximum area, then choose the one with the largest perimeter.

For example, if \(S = (8, 9, 14, 9, 27)\), then we can take the elements \((9, 14, 9, 27)\) and form an isosceles trapeziumAn isosceles trapezium (US: trapezoid) is a quadrilateral where one pair of opposite sides are parallel and of different lengths, and the other pair has the same length. with parallel side lengths \(14\) and \(27\) and both leg lengths \(9\). The area of this quadrilateral is \(127.611470879\cdots\) It can be shown that this is the largest area for any quadrilateral that can be formed using side lengths from \(S\). Therefore, \(f(S) = 9 + 14 + 9 + 27 = 59\).

Let \(u_n = 2^{B(3n)} + 3^{B(2n)} + B(n + 1)\), where \(B(k)\) is the number of \(1\) bits of \(k\) in base \(2\).
For example, \(B(6) = 2\), \(B(10) = 2\) and \(B(15) = 4\), and \(u_5 = 2^4 + 3^2 + 2 = 27\).

Also, let \(U_n\) be the sequence \((u_1, u_2, \dots, u_n)\).
For example, \(U_{10} = (8, 9, 14, 9, 27, 16, 36, 9, 27, 28)\).

It can be shown that \(f(U_5) = 59\), \(f(U_{10}) = 118\), \(f(U_{150}) = 3223\).
It can also be shown that \(\sum f(U_n) = 234761\) for \(4 \le n \le 150\).
Find \(\sum f(U_n)\) for \(4 \le n \le 3\,000\,000\).

This problem is taken from Project Euler, Problem 538.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=538. Published Sunday, 13th December 2015, 01:00 am. Solved by 388 members at time of mirroring.

Why this is useful

Optimization. A genuine objective is optimised over a choice set - the same shape as calibration and optimal-execution problems (Phases 10, 16).

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.