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/bell-numbers/bell-numbers.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 / bell-numbers / bell-numbers.om` 113 lines ยท 2222 bytes Raw Blame History 1 note Rosetta Code task "Bell numbers": https://rosettacode.org/wiki/Bell_numbers 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text tri "first 10 rows of the Bell triangle:\n" 7 text first "first 15 Bell numbers:" 8 text b49 "B(49), the 50th Bell number: " 9 note a triangle row of up to 50 entries; entry e is 6 limbs base 10^9 at 6e..6e+5, least significant first 10 note 6 limbs hold below 10^54; the largest entry needed, in row 49, is below B(50) < 2 * 10^47 11 global pv ulong 300 12 global cu ulong 300 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 row i of the Bell triangle starts with the last entry of row i - 1; each next entry adds the entry 40 note to its left and the entry above that one. Row i - 1 sits in pv and row i is built in cu, then copied 41 fn bellrow i ulong to void 42 for l ulong 6 43 put cu l get pv add mul sub i 1 6 l 44 end 45 for j ulong i 46 c = ulong 0 47 for l ulong 6 48 o = add mul j 6 l 49 s = add get cu o c 50 s = add s get pv o 51 put cu add o 6 rem s `1000000000` 52 c = div s `1000000000` 53 end 54 end 55 for t ulong mul add i 1 6 56 put pv t get cu t 57 end 58 end 59 note pv entry 0 in decimal: the top non-zero limb, then each lower limb padded to 9 digits 60 fn emitb to void 61 k = ulong 5 62 while and gt k 0 eq get pv k 0 bound 6 63 k = sub k 1 64 end 65 emitu get pv k 66 for t ulong k 67 v = get pv sub sub k 1 t 68 d = ulong `100000000` 69 while gt d 0 bound 9 70 emitu rem div v d 10 71 d = div d 10 72 end 73 end 74 end 75 fn main to int 76 bs = new ulong 15 0 77 put pv 0 1 78 put bs 0 1 79 emit tri 80 emitu 1 81 emit nl 82 i = ulong 1 83 while le i 49 bound 49 84 bellrow i 85 if lt i 10 86 for j ulong add i 1 87 if gt j 0 88 emit sp 89 end 90 emitu get pv mul j 6 91 end 92 emit nl 93 end 94 if lt i 15 95 put bs i get pv 0 96 end 97 if lt i 49 98 i = add i 1 99 else 100 break 101 end 102 end 103 emit first 104 for j ulong 15 105 emit sp 106 emitu get bs j 107 end 108 emit nl 109 emit b49 110 emitb 111 emit nl 112 ret 0 113 end