interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / pythagorean-triples / oracle.py

19 lines · 787 bytes Raw Blame History
1# Independent reference for "Pythagorean triples" (Python 3, standard library only).
2# expected.txt is this program's stdout; omc never produced it.
3import math
4# Berggren's tree of primitive triples: every primitive triple appears once, children have larger perimeters
5M = [((1, -2, 2), (2, -1, 2), (2, -2, 3)), ((1, 2, 2), (2, 1, 2), (2, 2, 3)), ((-1, 2, 2), (-2, 1, 2), (-2, 2, 3))]
6TOP = 10 ** 7
7per = []
8stack = [(3, 4, 5)]
9while stack:
10 t = stack.pop()
11 p = sum(t)
12 if p > TOP:
13 continue
14 per.append(p)
15 for m in M:
16 stack.append(tuple(sum(r[i] * t[i] for i in range(3)) for r in m))
17for e in range(2, 8):
18 lim = 10 ** e
19 print(f"perimeter <= {lim}: {sum(lim // p for p in per if p <= lim)} triples, {sum(1 for p in per if p <= lim)} primitive")