Project Euler Lab - Problem 165

#165 - Intersections

● AdvancedOfficial difficulty: 48%Algorithmic geometry: hullsTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

A segment is uniquely defined by its two endpoints.
By considering two line segments in plane geometry there are three possibilities:
the segments have zero points, one point, or infinitely many points in common.

Moreover when two segments have exactly one point in common it might be the case that that common point is an endpoint of either one of the segments or of both. If a common point of two segments is not an endpoint of either of the segments it is an interior point of both segments.
We will call a common point \(T\) of two segments \(L_1\) and \(L_2\) a true intersection point of \(L_1\) and \(L_2\) if \(T\) is the only common point of \(L_1\) and \(L_2\) and \(T\) is an interior point of both segments.

Consider the three segments \(L_1\), \(L_2\), and \(L_3\):

  • \(L_1\): \((27, 44)\) to \((12, 32)\)
  • \(L_2\): \((46, 53)\) to \((17, 62)\)
  • \(L_3\): \((46, 70)\) to \((22, 40)\)

It can be verified that line segments \(L_2\) and \(L_3\) have a true intersection point. We note that as the one of the end points of \(L_3\): \((22,40)\) lies on \(L_1\) this is not considered to be a true point of intersection. \(L_1\) and \(L_2\) have no common point. So among the three line segments, we find one true intersection point.

Now let us do the same for \(5000\) line segments. To this end, we generate \(20000\) numbers using the so-called "Blum Blum Shub" pseudo-random number generator.

\[\begin{align} s_0 &= 290797\\ s_{n + 1} &= s_n \times s_n \pmod{50515093}\\ t_n &= s_n \pmod{500} \end{align}\]

To create each line segment, we use four consecutive numbers \(t_n\). That is, the first line segment is given by:

\((t_1, t_2)\) to \((t_3, t_4)\).

The first four numbers computed according to the above generator should be: \(27\), \(144\), \(12\) and \(232\). The first segment would thus be \((27,144)\) to \((12,232)\).

How many distinct true intersection points are found among the \(5000\) line segments?

This problem is taken from Project Euler, Problem 165.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=165. Published Saturday, 27th October 2007, 10:00 am. Solved by 3,108 members at time of mirroring.

Why this is useful

Probability Statistics. Expectation and state-based probability reasoning underpin pricing, risk, and statistical inference (Phases 7, 11, 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.