interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / count-the-coins.wiki

79 lines · 1804 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text t1 "ways to make 100 cents from 1, 5, 10, 25: "
10text t2 "ways to make 100000 cents from 1, 5, 10, 25, 50, 100: "
11global ways ulong 100001
12global coins ulong 6
13note emit writes every byte of a to stdout; a short write is retried, an error exits 1
14fn emit a view uchar * to void
15o = ulong 0
16while lt o len a bound 4096
17k = write 1 a o sub len a o
18if le k 0
19exit 1
20end
21o = add o ulong k
22end
23end
24note emitu writes n in decimal; 20 digits hold every ulong
25fn emitu n ulong to void
26d = new uchar 20 0
27i = ulong 20
28v = n
29while true bound 20
30i = sub i 1
31put d i uchar add rem v 10 48
32v = div v 10
33if eq v 0
34break
35end
36end
37emit part d i sub 20 i
38end
39note addcoin lets every amount from c up to amt also end with a coin c
40fn addcoin amt ulong c ulong to void
41for x ulong add sub amt c 1
42y = add x c
43w = add get ways y get ways x
44put ways y w
45end
46end
47note count is the classic change-making dynamic program: ways[a] counts the multisets of the first k
48note coins that sum to a; adding coin c lets every amount a >= c also end with a c
49fn count amt ulong k ulong to ulong
50for a ulong add amt 1
51put ways a 0
52end
53put ways 0 1
54for i ulong k
55addcoin amt get coins i
56end
57ret get ways amt
58end
59fn main to int
60put coins 0 1
61put coins 1 5
62put coins 2 10
63put coins 3 25
64put coins 4 50
65put coins 5 100
66emit t1
67emitu count 100 4
68emit nl
69emit t2
70emitu count 100000 6
71emit nl
72ret 0
73end
74</syntaxhighlight>
75{{out}}
76<pre>
77ways to make 100 cents from 1, 5, 10, 25: 242
78ways to make 100000 cents from 1, 5, 10, 25, 50, 100: 13398445413854501
79</pre>