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 / count-the-coins / count-the-coins.om

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