Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/knapsack-problem-unbounded.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / knapsack-problem-unbounded.wiki`
99 lines ยท 2167 bytes Raw Blame History
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 (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text thead "panacea ichor gold value weight volume\n"
10 text ttenth "/10 "
11 text tthou "/1000\n"
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 note the weights are in tenths (0.3, 0.2, 2.0 -> 3, 2, 20; limit 25 -> 250) and the volumes in
39 note thousandths (0.025, 0.015, 0.002 -> 25, 15, 2; limit 0.25 -> 250), so all arithmetic is exact;
40 note values are 3000, 1800 and 2500; every count that fits is tried (at most 11 * 17 * 13 cases)
41 fn fits a ulong b ulong c ulong to bool
42 w = add add mul a 3 mul b 2 mul c 20
43 v = add add mul a 25 mul b 15 mul c 2
44 ret and le w 250 le v 250
45 end
46 fn value a ulong b ulong c ulong to ulong
47 ret add add mul a 3000 mul b 1800 mul c 2500
48 end
49 fn row a ulong b ulong c ulong to void
50 emitu a
51 emit sp
52 emitu b
53 emit sp
54 emitu c
55 emit sp
56 emitu value a b c
57 emit sp
58 emitu add add mul a 3 mul b 2 mul c 20
59 emit ttenth
60 emitu add add mul a 25 mul b 15 mul c 2
61 emit tthou
62 end
63 note pass 1 finds the best value, pass 2 prints every combination that reaches it
64 fn main to int
65 best = ulong 0
66 for a ulong 11
67 for b ulong 17
68 for c ulong 13
69 if fits a b c
70 if gt value a b c best
71 best = value a b c
72 end
73 end
74 end
75 end
76 end
77 emit thead
78 for a ulong 11
79 for b ulong 17
80 for c ulong 13
81 if fits a b c
82 if eq value a b c best
83 row a b c
84 end
85 end
86 end
87 end
88 end
89 ret 0
90 end
91
92 {{out}}
93
94 panacea ichor gold value weight volume
95 0 15 11 54500 250/10 247/1000
96 3 10 11 54500 249/10 247/1000
97 6 5 11 54500 248/10 247/1000
98 9 0 11 54500 247/10 247/1000
99