omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / sum-and-product-of-an-array / sum-and-product-of-an-array.om
| 1 | note Rosetta Code task "Sum and product of an array": https://rosettacode.org/wiki/Sum_and_product_of_an_array |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text tsum "sum " |
| 6 | text tprod "product " |
| 7 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 8 | fn emit a view uchar * to void |
| 9 | o = ulong 0 |
| 10 | while lt o len a bound 4096 |
| 11 | k = write 1 a o sub len a o |
| 12 | if le k 0 |
| 13 | exit 1 |
| 14 | end |
| 15 | o = add o ulong k |
| 16 | end |
| 17 | end |
| 18 | note emitu writes n in decimal; 20 digits hold every ulong |
| 19 | fn emitu n ulong to void |
| 20 | d = new uchar 20 0 |
| 21 | i = ulong 20 |
| 22 | v = n |
| 23 | while true bound 20 |
| 24 | i = sub i 1 |
| 25 | put d i uchar add rem v 10 48 |
| 26 | v = div v 10 |
| 27 | if eq v 0 |
| 28 | break |
| 29 | end |
| 30 | end |
| 31 | emit part d i sub 20 i |
| 32 | end |
| 33 | fn main to int |
| 34 | a = new ulong 10 0 |
| 35 | for i ulong 10 |
| 36 | put a i add i 1 |
| 37 | end |
| 38 | s = ulong 0 |
| 39 | p = ulong 1 |
| 40 | for i ulong 10 |
| 41 | s = add s get a i |
| 42 | p = mul p get a i |
| 43 | end |
| 44 | emit tsum |
| 45 | emitu s |
| 46 | emit nl |
| 47 | emit tprod |
| 48 | emitu p |
| 49 | emit nl |
| 50 | ret 0 |
| 51 | end |