omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / ethiopian-multiplication.wiki
| 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 (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text kept " KEPT\n" |
| 9 | text struck " STRUCK\n" |
| 10 | text tx " x " |
| 11 | text teq " = " |
| 12 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 13 | fn emit a view uchar * to void |
| 14 | o = ulong 0 |
| 15 | while lt o len a bound 4096 |
| 16 | k = write 1 a o sub len a o |
| 17 | if le k 0 |
| 18 | exit 1 |
| 19 | end |
| 20 | o = add o ulong k |
| 21 | end |
| 22 | end |
| 23 | note emitu writes n in decimal; 20 digits hold every ulong |
| 24 | fn emitu n ulong to void |
| 25 | d = new uchar 20 0 |
| 26 | i = ulong 20 |
| 27 | v = n |
| 28 | while true bound 20 |
| 29 | i = sub i 1 |
| 30 | put d i uchar add rem v 10 48 |
| 31 | v = div v 10 |
| 32 | if eq v 0 |
| 33 | break |
| 34 | end |
| 35 | end |
| 36 | emit part d i sub 20 i |
| 37 | end |
| 38 | fn halve n ulong to ulong |
| 39 | ret shr n 1 |
| 40 | end |
| 41 | fn double n ulong to ulong |
| 42 | ret shl n 1 |
| 43 | end |
| 44 | fn iseven n ulong to bool |
| 45 | ret eq and n 1 0 |
| 46 | end |
| 47 | note left column halves to 1, right column doubles; rows with an even left number are struck out and |
| 48 | note the kept right numbers add up to the product |
| 49 | fn ethiop a ulong b ulong to ulong |
| 50 | s = ulong 0 |
| 51 | l = a |
| 52 | r = b |
| 53 | while ge l 1 bound 64 |
| 54 | emitu l |
| 55 | emit sp |
| 56 | emitu r |
| 57 | if iseven l |
| 58 | emit struck |
| 59 | else |
| 60 | emit kept |
| 61 | s = add s r |
| 62 | end |
| 63 | l = halve l |
| 64 | r = double r |
| 65 | end |
| 66 | ret s |
| 67 | end |
| 68 | fn main to int |
| 69 | p = ethiop 17 34 |
| 70 | emitu 17 |
| 71 | emit tx |
| 72 | emitu 34 |
| 73 | emit teq |
| 74 | emitu p |
| 75 | emit nl |
| 76 | ret 0 |
| 77 | end |
| 78 | </syntaxhighlight> |
| 79 | {{out}} |
| 80 | <pre> |
| 81 | 17 34 KEPT |
| 82 | 8 68 STRUCK |
| 83 | 4 136 STRUCK |
| 84 | 2 272 STRUCK |
| 85 | 1 544 KEPT |
| 86 | 17 x 34 = 578 |
| 87 | </pre> |