interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / evaluate-binomial-coefficients.wiki

84 lines · 1573 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text cc "C("
8text cm ", "
9text eqs ") = "
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36fn gcd a ulong b ulong to ulong
37x = a
38y = b
39while ne y 0 bound 128
40t = rem x y
41x = y
42y = t
43end
44ret x
45end
46note r runs through C(n-k+i, i) for i = 1..k; with g = gcd(r, i), i / g divides n - k + i, so
47note r / g * ((n-k+i) / (i / g)) is exact and never exceeds the final value, which must fit 64 bits
48fn binom n ulong k ulong to ulong
49r = ulong 1
50for j ulong k
51i = add j 1
52g = gcd r i
53r = mul div r g div add sub n k i div i g
54end
55ret r
56end
57fn tell n ulong k ulong to void
58emit cc
59emitu n
60emit cm
61emitu k
62emit eqs
63emitu binom n k
64emit nl
65end
66fn main to int
67tell 5 3
68tell 10 0
69tell 10 10
70tell 33 17
71tell 60 30
72tell 67 33
73ret 0
74end
75</syntaxhighlight>
76{{out}}
77<pre>
78C(5, 3) = 10
79C(10, 0) = 1
80C(10, 10) = 1
81C(33, 17) = 1166803110
82C(60, 30) = 118264581564861424
83C(67, 33) = 14226520737620288370
84</pre>