interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / bell-numbers / bell-numbers.om

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