Project Euler Lab - Problem 102

#102 - Triangle Containment

● FoundationOfficial difficulty: 8%RandomnessTier A - full browser executionNot viewed
↖ Euler Lab

Three distinct points are plotted at random on a Cartesian plane, for which \(-1000 \le x, y \le 1000\), such that a triangle is formed.

Consider the following two triangles:

\[\begin{gather} A(-340,495), B(-153,-910), C(835,-947)\\ X(-175,41), Y(-421,-714), Z(574,-645) \end{gather}\]

It can be verified that triangle \(ABC\) contains the origin, whereas triangle \(XYZ\) does not.

Using triangles.txt (right click and 'Save Link/Target As...'), a 27K text file containing the co-ordinates of one thousand "random" triangles, find the number of triangles for which the interior contains the origin.

NOTE: The first two examples in the file represent the triangles in the example given above.

This problem is taken from Project Euler, Problem 102.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=102. Published Friday, 12th August 2005, 06:00 pm. Solved by 24,737 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 A - full browser execution
Runs at full original scale within the browser limits.

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.

This problem has a data file
The dataset 0102_triangles.txt is bundled locally and pre-loaded into your Python environment as DATA (a string) and DATA_LINES (a list of lines). No download or network access is needed. Download the file.
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.