This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1 @@
println[binomial[5,3]]

View file

@ -0,0 +1,3 @@
# Built-in
Binomial(5, 3);
# 10

View file

@ -0,0 +1,3 @@
;5 3 # Set up demo input
{),(;{*}*}:f; # Define a factorial function
.f@.f@/\@-f/

View file

@ -0,0 +1,2 @@
;5 3 # Set up demo input
1\,@{1$-@\*\)/}+/

View file

@ -0,0 +1,10 @@
def factorial = { x ->
assert x > -1
x == 0 ? 1 : (1..x).inject(1G) { BigInteger product, BigInteger factor -> product *= factor }
}
def combinations = { n, k ->
assert k >= 0
assert n >= k
factorial(n).intdiv(factorial(k)*factorial(n-k))
}

View file

@ -0,0 +1,4 @@
assert combinations(20, 0) == combinations(20, 20)
assert combinations(20, 10) == (combinations(19, 9) + combinations(19, 10))
assert combinations(5, 3) == 10
println combinations(5, 3)

View file

@ -0,0 +1,12 @@
WRITE(Messagebox) BinomCoeff( 5, 3) ! displays 10
FUNCTION factorial( n )
factorial = 1
DO i = 1, n
factorial = factorial * i
ENDDO
END
FUNCTION BinomCoeff( n, k )
BinomCoeff = factorial(n)/factorial(n-k)/factorial(k)
END

View file

@ -0,0 +1,5 @@
link math, factors
procedure main()
write("choose(5,3)=",binocoef(5,3))
end

View file

@ -0,0 +1,27 @@
procedure binocoef(n, k) #: binomial coefficient
k := integer(k) | fail
n := integer(n) | fail
if (k = 0) | (n = k) then return 1
if 0 <= k <= n then
return factorial(n) / (factorial(k) * factorial(n - k))
else fail
end
procedure factorial(n) #: return n! (n factorial)
local i
n := integer(n) | runerr(101, n)
if n < 0 then fail
i := 1
every i *:= 1 to n
return i
end

View file

@ -0,0 +1,2 @@
3 ! 5
10

View file

@ -0,0 +1,2 @@
{[n;k]_(*/(k-1)_1+!n)%(*/1+!k)} . 5 3
10

View file

@ -0,0 +1,2 @@
{[n;k]i:!(k-1);_*/((n-i)%(i+1))} . 5 3
10

View file

@ -0,0 +1,11 @@
pascal:{x{+':0,x,0}\1}
pascal 5
(1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1)
{[n;k](pascal n)[n;k]} . 5 3
10

View file

@ -0,0 +1,21 @@
' [RC] Binomial Coefficients
print "Binomial Coefficient of "; 5; " and "; 3; " is ",BinomialCoefficient( 5, 3)
n =1 +int( 10 *rnd( 1))
k =1 +int( n *rnd( 1))
print "Binomial Coefficient of "; n; " and "; k; " is ",BinomialCoefficient( n, k)
end
function BinomialCoefficient( n, k)
BinomialCoefficient =factorial( n) /factorial( n -k) /factorial( k)
end function
function factorial( n)
if n <2 then
f =1
else
f =n *factorial( n -1)
end if
factorial =f
end function

View file

@ -0,0 +1,7 @@
to choose :n :k
if :k = 0 [output 1]
output (choose :n :k-1) * (:n - :k + 1) / :k
end
show choose 5 3 ; 10
show choose 60 30 ; 1.18264581564861e+17

View file

@ -0,0 +1,2 @@
(Local) In[1]:= Binomial[5,3]
(Local) Out[1]= 10

View file

@ -0,0 +1,13 @@
binomial( 5, 3); /* 10 */
binomial(-5, 3); /* -35 */
binomial( 5, -3); /* 0 */
binomial(-5, -3); /* 0 */
binomial( 3, 5); /* 0 */
binomial(x, 3); /* ((x - 2)*(x - 1)*x)/6 */
binomial(3, 1/2); /* binomial(3, 1/2) */
makegamma(%); /* 32/(5*%pi) */
binomial(a, b); /* binomial(a, b) */
makegamma(%); /* gamma(a + 1)/(gamma(-b + a + 1)*gamma(b + 1)) */