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 / count-the-coins / count-the-coins.om
| 1 | note Rosetta Code task "Count the coins": https://rosettacode.org/wiki/Count_the_coins |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text minus "-" |
| 7 | text t1 "ways to make 100 cents from 1, 5, 10, 25: " |
| 8 | text t2 "ways to make 100000 cents from 1, 5, 10, 25, 50, 100: " |
| 9 | global ways ulong 100001 |
| 10 | global coins ulong 6 |
| 11 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 12 | fn emit a view uchar * to void |
| 13 | o = ulong 0 |
| 14 | while lt o len a bound 4096 |
| 15 | k = write 1 a o sub len a o |
| 16 | if le k 0 |
| 17 | exit 1 |
| 18 | end |
| 19 | o = add o ulong k |
| 20 | end |
| 21 | end |
| 22 | note emitu writes n in decimal; 20 digits hold every ulong |
| 23 | fn emitu n ulong to void |
| 24 | d = new uchar 20 0 |
| 25 | i = ulong 20 |
| 26 | v = n |
| 27 | while true bound 20 |
| 28 | i = sub i 1 |
| 29 | put d i uchar add rem v 10 48 |
| 30 | v = div v 10 |
| 31 | if eq v 0 |
| 32 | break |
| 33 | end |
| 34 | end |
| 35 | emit part d i sub 20 i |
| 36 | end |
| 37 | note addcoin lets every amount from c up to amt also end with a coin c |
| 38 | fn addcoin amt ulong c ulong to void |
| 39 | for x ulong add sub amt c 1 |
| 40 | y = add x c |
| 41 | w = add get ways y get ways x |
| 42 | put ways y w |
| 43 | end |
| 44 | end |
| 45 | note count is the classic change-making dynamic program: ways[a] counts the multisets of the first k |
| 46 | note coins that sum to a; adding coin c lets every amount a >= c also end with a c |
| 47 | fn count amt ulong k ulong to ulong |
| 48 | for a ulong add amt 1 |
| 49 | put ways a 0 |
| 50 | end |
| 51 | put ways 0 1 |
| 52 | for i ulong k |
| 53 | addcoin amt get coins i |
| 54 | end |
| 55 | ret get ways amt |
| 56 | end |
| 57 | fn main to int |
| 58 | put coins 0 1 |
| 59 | put coins 1 5 |
| 60 | put coins 2 10 |
| 61 | put coins 3 25 |
| 62 | put coins 4 50 |
| 63 | put coins 5 100 |
| 64 | emit t1 |
| 65 | emitu count 100 4 |
| 66 | emit nl |
| 67 | emit t2 |
| 68 | emitu count 100000 6 |
| 69 | emit nl |
| 70 | ret 0 |
| 71 | end |