Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/pythagorean-triples.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / pythagorean-triples.wiki`
97 lines ยท 2029 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text pre "perimeter <= "
8 text col ": "
9 text tr " triples, "
10 text pr " primitive\n"
11 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12 fn emit a view uchar * to void
13 o = ulong 0
14 while lt o len a bound 4096
15 k = write 1 a o sub len a o
16 if le k 0
17 exit 1
18 end
19 o = add o ulong k
20 end
21 end
22 note emitu writes n in decimal; 20 digits hold every ulong
23 fn emitu n ulong to void
24 d = new uchar 20 0
25 i = ulong 20
26 v = n
27 while true bound 20
28 i = sub i 1
29 put d i uchar add rem v 10 48
30 v = div v 10
31 if eq v 0
32 break
33 end
34 end
35 emit part d i sub 20 i
36 end
37 fn gcd a ulong b ulong to ulong
38 x = a
39 y = b
40 while ne y 0 bound 128
41 t = rem x y
42 x = y
43 y = t
44 end
45 ret x
46 end
47 note Euclid: m > n > 0 coprime of opposite parity give each primitive triple once, with perimeter 2m(m+n);
48 note a primitive perimeter p counts for limit L once as primitive and L / p times among all triples
49 note bound: 2m(m+1) <= 10^7 needs m <= 2235
50 fn main to int
51 pc = new ulong 7 0
52 tc = new ulong 7 0
53 m = ulong 2
54 while le mul mul 2 m add m 1 `10000000` bound 2236
55 n = add rem m 2 1
56 while lt n m bound 2236
57 p = mul mul 2 m add m n
58 if gt p `10000000`
59 break
60 end
61 if eq gcd m n 1
62 l = ulong 100
63 for k ulong 6
64 if le p l
65 put pc k add get pc k 1
66 put tc k add get tc k div l p
67 end
68 l = mul l 10
69 end
70 end
71 n = add n 2
72 end
73 m = add m 1
74 end
75 l = ulong 100
76 for k ulong 6
77 emit pre
78 emitu l
79 emit col
80 emitu get tc k
81 emit tr
82 emitu get pc k
83 emit pr
84 l = mul l 10
85 end
86 ret 0
87 end
88
89 {{out}}
90
91 perimeter <= 100: 17 triples, 7 primitive
92 perimeter <= 1000: 325 triples, 70 primitive
93 perimeter <= 10000: 4858 triples, 703 primitive
94 perimeter <= 100000: 64741 triples, 7026 primitive
95 perimeter <= `1000000`: 808950 triples, 70229 primitive
96 perimeter <= `10000000`: `9706567` triples, 702309 primitive
97