Evaluate binomial coefficients: Difference between revisions

m
→‎{{header|Oberon}}: Fixed language name
m (syntax highlighting fixup automation)
m (→‎{{header|Oberon}}: Fixed language name)
 
(7 intermediate revisions by 5 users not shown)
Line 756:
(100 50) = 1976664223067613962806675336</pre>
 
The above wouldn't work for me (100C50 correctly gives 100891344545564193334812497256). This next one is a translation of C#:
 
{{trans|C#}}
 
<syntaxhighlight lang="d">T BinomialCoeff(T)(in T n, in T k)
{
Line 782 ⟶ 785:
<pre>120
100891344545564193334812497256</pre>
 
 
 
 
 
 
 
=={{header|dc}}==
Line 861 ⟶ 858:
ReadLn;
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func binomial n k .
if k > n / 2
k = n - k
.
numer = 1
for i = n downto n - k + 1
numer = numer * i
.
denom = 1
for i = 1 to k
denom = denom * i
.
return numer / denom
.
print binomial 5 3
</syntaxhighlight>
 
=={{header|Elixir}}==
Line 1,895 ⟶ 1,911:
<pre>10</pre>
 
=={{header|Oberon-2}}==
{{works with|oo2c}}
<syntaxhighlight lang="oberon2">
Line 2,457 ⟶ 2,473:
return numer
</syntaxhighlight>
 
=={{header|RPL}}==
RPL has a <code>COMB</code> instruction which returns the result directly. If a home-made function is preferred, there are many possibilities.
 
Using the recommended formula and the stack:
≪ - LAST ! SWAP ! SWAP ROT ! * / ≫ ‘'''CHOOS'''’ STO
Using the formula with local variables:
≪ → n k ≪ n ! n k - ! / k ! / ≫ ≫ ‘'''CHOOS'''’ STO
To avoid data overflow for high values of n, using the formula simplified by (n-k)! :
≪ → n k ≪ 1 n k - 1 + n '''FOR''' j j * '''NEXT''' k ! / ≫ ≫ ‘'''CHOOS'''’ STO
All the above functions are called the same way and return the same result:
5 3 '''CHOOS'''
{{out}}
<pre>
10
</pre>
 
=={{header|Ruby}}==
Line 2,737 ⟶ 2,769:
<syntaxhighlight lang="smalltalk">binco:arg
^ (self factorial) / (arg factorial * (self-arg) factorial)</syntaxhighlight>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="standardml">
fun binomial n k =
if k > n then 0 else
let fun f (_, 0) = 1
| f (i, d) = f (i + 1, d - 1) * i div d
in f (n - k + 1, k) end
</syntaxhighlight>
 
=={{header|Stata}}==
Line 2,794 ⟶ 2,835:
<pre>5_C_3 = 10
60_C_30 = 118264581564861424</pre>
 
=={{header|TI-57}}==
{| class="wikitable"
! Machine code
! Comment
|-
|
'''Lbl 9'''
STO 2
x⮂t
STO 1
SBR 0
STO 3
RCL 1
-
RCL 2
=
SBR 0
INV Prd 3
RCL 2
SBR 0
Inv Prd 3
RCL 3
R/S
RST
'''Lbl 0'''
C.t
x=t
1
STO 0
'''Lbl 1'''
RCL 0
×
Dsz
GTO 1
1
=
INV SBR
|
'''program binomial(x,t)''' // x is the display register
r2 = x
swap x and t
r1 = x
x = factorial(x)
r3 = x
x = r1 - r2
x = factorial(x)
r3 /= x
x = r2
x = factorial(x)
r3 /= x
x = r3
end program
reset pointer
'''program factorial(x)'''
t=0
if x=0 then
x=1
r0 = x
loop
multiply r0 by what will be in the next loop
decrement r0 and exit loop if r0 = 0
end loop
complete the multiplication sequence
return x!
end sub
|}
<code> 5 </code> <code>x⮂t</code> <code> 3 </code> <code>GTO</code> <code> 9 </code> <code>R/S</code>
{{out}}
<pre>
10.
</pre>
 
=={{header|TI-83 BASIC}}==
Line 2,901 ⟶ 3,018:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int
 
var binomial = Fn.new { |n, k|
3,021

edits