interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / greatest-common-divisor / greatest-common-divisor.om

62 lines · 1123 bytes Raw Blame History
1note Rosetta Code task "Greatest common divisor": https://rosettacode.org/wiki/Greatest_common_divisor
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text pre "gcd("
6text cm ", "
7text post ") = "
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note emitu writes n in decimal; 20 digits hold every ulong
20fn emitu n ulong to void
21d = new uchar 20 0
22i = ulong 20
23v = n
24while true bound 20
25i = sub i 1
26put d i uchar add rem v 10 48
27v = div v 10
28if eq v 0
29break
30end
31end
32emit part d i sub 20 i
33end
34note Euclid; by Lame's theorem two 64-bit numbers need fewer than 100 steps
35fn gcd a ulong b ulong to ulong
36x = a
37y = b
38while ne y 0 bound 100
39t = rem x y
40x = y
41y = t
42end
43ret x
44end
45fn show a ulong b ulong to void
46emit pre
47emitu a
48emit cm
49emitu b
50emit post
51emitu gcd a b
52emit nl
53end
54fn main to int
55show 1071 1029
56show 40902 24140
57show 12 18
58show 17 5
59show 0 5
60show 18446744073709551615 6148914691236517205
61ret 0
62end