interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / greatest-common-divisor.wiki

74 lines · 1395 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text pre "gcd("
8text cm ", "
9text post ") = "
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note Euclid; by Lame's theorem two 64-bit numbers need fewer than 100 steps
37fn gcd a ulong b ulong to ulong
38x = a
39y = b
40while ne y 0 bound 100
41t = rem x y
42x = y
43y = t
44end
45ret x
46end
47fn show a ulong b ulong to void
48emit pre
49emitu a
50emit cm
51emitu b
52emit post
53emitu gcd a b
54emit nl
55end
56fn main to int
57show 1071 1029
58show 40902 24140
59show 12 18
60show 17 5
61show 0 5
62show 18446744073709551615 6148914691236517205
63ret 0
64end
65</syntaxhighlight>
66{{out}}
67<pre>
68gcd(1071, 1029) = 21
69gcd(40902, 24140) = 34
70gcd(12, 18) = 6
71gcd(17, 5) = 1
72gcd(0, 5) = 5
73gcd(18446744073709551615, 6148914691236517205) = 6148914691236517205
74</pre>