Project Euler Lab - Problem 149

#149 - Maximum-sum Subsequence

● AppliedOfficial difficulty: 27%PointsTier B - browser, with the efficient algorithmNot viewed
↖ Euler Lab

Looking at the table below, it is easy to verify that the maximum possible sum of adjacent numbers in any direction (horizontal, vertical, diagonal or anti-diagonal) is \(16\) (\(= 8 + 7 + 1\)).

\(-2\)\(5\)\(3\)\(2\)
\(9\)\(-6\)\(5\)\(1\)
\(3\)\(2\)\(7\)\(3\)
\(-1\)\(8\)\(-4\)\(8\)

Now, let us repeat the search, but on a much larger scale:

First, generate four million pseudo-random numbers using a specific form of what is known as a "Lagged Fibonacci Generator":

For \(1 \le k \le 55\), \(s_k = [100003 - 200003 k + 300007 k^3] \pmod{1000000} - 500000\).
For \(56 \le k \le 4000000\), \(s_k = [s_{k-24} + s_{k - 55} + 1000000] \pmod{1000000} - 500000\).

Thus, \(s_{10} = -393027\) and \(s_{100} = 86613\).

The terms of \(s\) are then arranged in a \(2000 \times 2000\) table, using the first \(2000\) numbers to fill the first row (sequentially), the next \(2000\) numbers to fill the second row, and so on.

Finally, find the greatest sum of (any number of) adjacent entries in any direction (horizontal, vertical, diagonal or anti-diagonal).

This problem is taken from Project Euler, Problem 149.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=149. Published Friday, 13th April 2007, 10:00 pm. Solved by 5,649 members at time of mirroring.

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 B - browser, with the efficient algorithm
Runs in the browser only with the intended efficient algorithm; a naive loop will hit the timeout.

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.