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