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 / pythagorean-triples.om

85 lines · 1592 bytes Raw Blame History
1note Rosetta Code task "Pythagorean triples": https://rosettacode.org/wiki/Pythagorean_triples
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text pre "perimeter <= "
6text col ": "
7text tr " triples, "
8text pr " primitive\n"
9note emit writes every byte of a to stdout; a short write is retried, an error exits 1
10fn emit a view uchar * to void
11o = ulong 0
12while lt o len a bound 4096
13k = write 1 a o sub len a o
14if le k 0
15exit 1
16end
17o = add o ulong k
18end
19end
20note emitu writes n in decimal; 20 digits hold every ulong
21fn emitu n ulong to void
22d = new uchar 20 0
23i = ulong 20
24v = n
25while true bound 20
26i = sub i 1
27put d i uchar add rem v 10 48
28v = div v 10
29if eq v 0
30break
31end
32end
33emit part d i sub 20 i
34end
35fn gcd a ulong b ulong to ulong
36x = a
37y = b
38while ne y 0 bound 128
39t = rem x y
40x = y
41y = t
42end
43ret x
44end
45note Euclid: m > n > 0 coprime of opposite parity give each primitive triple once, with perimeter 2m(m+n);
46note a primitive perimeter p counts for limit L once as primitive and L / p times among all triples
47note bound: 2m(m+1) <= 10^7 needs m <= 2235
48fn main to int
49pc = new ulong 7 0
50tc = new ulong 7 0
51m = ulong 2
52while le mul mul 2 m add m 1 10000000 bound 2236
53n = add rem m 2 1
54while lt n m bound 2236
55p = mul mul 2 m add m n
56if gt p 10000000
57break
58end
59if eq gcd m n 1
60l = ulong 100
61for k ulong 6
62if le p l
63put pc k add get pc k 1
64put tc k add get tc k div l p
65end
66l = mul l 10
67end
68end
69n = add n 2
70end
71m = add m 1
72end
73l = ulong 100
74for k ulong 6
75emit pre
76emitu l
77emit col
78emitu get tc k
79emit tr
80emitu get pc k
81emit pr
82l = mul l 10
83end
84ret 0
85end