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-mv/blob/main/si/rosetta/tasks/equilibrium-index/equilibrium-index.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / equilibrium-index / equilibrium-index.om` 122 lines ยท 2165 bytes Raw Blame History 1 note Rosetta Code task "Equilibrium index": https://rosettacode.org/wiki/Equilibrium_index 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 d1 "-7 1 5 2 -4 3 0" 8 text d2 "2 4 6" 9 text d3 "2 9 2" 10 text d4 "1 -1 1 -1 1 -1 1" 11 text tidx "equilibrium indices:" 12 global vals long 16 13 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 14 fn emit a view uchar * to void 15 o = ulong 0 16 while lt o len a bound 4096 17 k = write 1 a o sub len a o 18 if le k 0 19 exit 1 20 end 21 o = add o ulong k 22 end 23 end 24 note emitu writes n in decimal; 20 digits hold every ulong 25 fn emitu n ulong to void 26 d = new uchar 20 0 27 i = ulong 20 28 v = n 29 while true bound 20 30 i = sub i 1 31 put d i uchar add rem v 10 48 32 v = div v 10 33 if eq v 0 34 break 35 end 36 end 37 emit part d i sub 20 i 38 end 39 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 40 fn emiti n long to void 41 if lt n 0 42 emit minus 43 emitu add bitcast ulong not n 1 44 else 45 emitu ulong n 46 end 47 end 48 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and 49 note returns how many; one pass over s, so the loop is bounded by len s 50 fn parse s view uchar * to ulong 51 n = ulong 0 52 v = long 0 53 sg = long 1 54 dig = false 55 for i ulong len s 56 c = get s i 57 if eq c 45 58 sg = long -1 59 else 60 if and ge c 48 le c 57 61 v = add mul v 10 long sub c 48 62 dig = true 63 else 64 if dig 65 put vals n mul sg v 66 n = add n 1 67 end 68 v = long 0 69 sg = long 1 70 dig = false 71 end 72 end 73 end 74 if dig 75 put vals n mul sg v 76 n = add n 1 77 end 78 ret n 79 end 80 note show writes vals[0..n-1] separated by spaces, then a newline 81 fn show n ulong to void 82 for i ulong n 83 if gt i 0 84 emit sp 85 end 86 emiti get vals i 87 end 88 emit nl 89 end 90 note eqidx keeps the sum to the left and the sum to the right of i in one pass after the total 91 fn eqidx n ulong to void 92 tot = long 0 93 for i ulong n 94 tot = add tot get vals i 95 end 96 lsum = long 0 97 emit tidx 98 for i ulong n 99 rsum = sub sub tot lsum get vals i 100 if eq lsum rsum 101 emit sp 102 emitu i 103 end 104 lsum = add lsum get vals i 105 end 106 emit nl 107 end 108 fn main to int 109 n = parse d1 110 show n 111 eqidx n 112 n = parse d2 113 show n 114 eqidx n 115 n = parse d3 116 show n 117 eqidx n 118 n = parse d4 119 show n 120 eqidx n 121 ret 0 122 end