Project Euler Lab - Problem 630

#630 - Crossed Lines

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

Given a set, \(L\), of unique lines, let \(M(L)\) be the number of lines in the set and let \(S(L)\) be the sum over every line of the number of times that line is crossed by another line in the set. For example, two sets of three lines are shown below:

crossed lines

In both cases \(M(L)\) is \(3\) and \(S(L)\) is \(6\): each of the three lines is crossed by two other lines. Note that even if the lines cross at a single point, all of the separate crossings of lines are counted.

Consider points \((T_{2k-1}, T_{2k})\), for integer \(k \ge 1\), generated in the following way:

\(S_0 = 290797\)
\(S_{n+1} = S_n^2 \bmod 50515093\)
\(T_n = (S_n \bmod 2000) - 1000\)

For example, the first three points are: \((527, 144)\), \((-488, 732)\), \((-454, -947)\). Given the first \(n\) points generated in this manner, let \(L_n\) be the set of unique lines that can be formed by joining each point with every other point, the lines being extended indefinitely in both directions. We can then define \(M(L_n)\) and \(S(L_n)\) as described above.

For example, \(M(L_3) = 3\) and \(S(L_3) = 6\). Also \(M(L_{100}) = 4948\) and \(S(L_{100}) = 24477690\).

Find \(S(L_{2500})\).

This problem is taken from Project Euler, Problem 630.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=630. Published Sunday, 1st July 2018, 07:00 am. Solved by 1,251 members at time of mirroring.

Why this is useful

General Problem Solving. Builds computational thinking, decomposition, and debugging discipline - transferable, without a specific financial application.

We classify relevance honestly - not every Euler problem is a trading application.

Prerequisites

Lessons that prepare you:
1.1 Sets, Functions, and Relations · 17.1 Python for Quants: NumPy, pandas, and Vectorization · 2.1 Functions, Limits, and Continuity · 4.2 Linear Maps, Matrices, Rank, and the Null Space

Recommended stepping-stone problems: #164 · #686 · #719

Concepts: algebra

Likely techniques: hashing

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.