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/tasks/knapsack-problem-unbounded/knapsack-problem-unbounded.om # 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 / tasks / knapsack-problem-unbounded / knapsack-problem-unbounded.om` 88 lines ยท 1902 bytes Raw Blame History 1 note Rosetta Code task "Knapsack problem/Unbounded": https://rosettacode.org/wiki/Knapsack_problem/Unbounded 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 thead "panacea ichor gold value weight volume\n" 8 text ttenth "/10 " 9 text tthou "/1000\n" 10 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 11 fn emit a view uchar * to void 12 o = ulong 0 13 while lt o len a bound 4096 14 k = write 1 a o sub len a o 15 if le k 0 16 exit 1 17 end 18 o = add o ulong k 19 end 20 end 21 note emitu writes n in decimal; 20 digits hold every ulong 22 fn emitu n ulong to void 23 d = new uchar 20 0 24 i = ulong 20 25 v = n 26 while true bound 20 27 i = sub i 1 28 put d i uchar add rem v 10 48 29 v = div v 10 30 if eq v 0 31 break 32 end 33 end 34 emit part d i sub 20 i 35 end 36 note the weights are in tenths (0.3, 0.2, 2.0 -> 3, 2, 20; limit 25 -> 250) and the volumes in 37 note thousandths (0.025, 0.015, 0.002 -> 25, 15, 2; limit 0.25 -> 250), so all arithmetic is exact; 38 note values are 3000, 1800 and 2500; every count that fits is tried (at most 11 * 17 * 13 cases) 39 fn fits a ulong b ulong c ulong to bool 40 w = add add mul a 3 mul b 2 mul c 20 41 v = add add mul a 25 mul b 15 mul c 2 42 ret and le w 250 le v 250 43 end 44 fn value a ulong b ulong c ulong to ulong 45 ret add add mul a 3000 mul b 1800 mul c 2500 46 end 47 fn row a ulong b ulong c ulong to void 48 emitu a 49 emit sp 50 emitu b 51 emit sp 52 emitu c 53 emit sp 54 emitu value a b c 55 emit sp 56 emitu add add mul a 3 mul b 2 mul c 20 57 emit ttenth 58 emitu add add mul a 25 mul b 15 mul c 2 59 emit tthou 60 end 61 note pass 1 finds the best value, pass 2 prints every combination that reaches it 62 fn main to int 63 best = ulong 0 64 for a ulong 11 65 for b ulong 17 66 for c ulong 13 67 if fits a b c 68 if gt value a b c best 69 best = value a b c 70 end 71 end 72 end 73 end 74 end 75 emit thead 76 for a ulong 11 77 for b ulong 17 78 for c ulong 13 79 if fits a b c 80 if eq value a b c best 81 row a b c 82 end 83 end 84 end 85 end 86 end 87 ret 0 88 end