Project Euler Lab - Problem 540

#540 - Counting Primitive Pythagorean Triples

● AdvancedOfficial difficulty: 56%DivisibilityTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

A Pythagorean triple consists of three positive integers \(a, b\) and \(c\) satisfying \(a^2+b^2=c^2\).
The triple is called primitive if \(a, b\) and \(c\) are relatively prime.
Let \(P(n)\) be the number of primitive Pythagorean triples with \(a \lt b \lt c \le n\).
For example \(P(20) = 3\), since there are three triples: \((3,4,5)\), \((5,12,13)\) and \((8,15,17)\).

You are given that \(P(10^6) = 159139\).
Find \(P(3141592653589793)\).

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

Why this is useful

Mathematical Foundation. Exact integer reasoning and algorithmic efficiency. Foundational rigour and computational discipline rather than a direct trading application.

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.