# Mathematical Combination

Write a program that takes an input such as:

n,k


which then computes:

$$\binom n k = \frac {n!} {k!(n-k)!}$$

and then prints the result.

A numerical example:

Input:

5,2


Internal computation:

$$\frac {5!} {3!\times2!}$$

Printed Output:

10


I'd like to see an answer that beats my python solution of 65 characters, but all languages are obviously welcome.

Here's my solution:

n,k=input();f=lambda x:+(x<2)or x*f(x-1);print f(n)/(f(k)*f(n-k))


Edit:

I admit that this question is from the codegolf website mathematical combination puzzle. I know that my answer may look like not much progress can be made on it, but the leaders of this puzzle have solved it in nearly half as many characters.

• Function or full program? Your description says function that returns the correct result, but your example is a full program that prints it. Mar 22, 2011 at 16:38
• @Ventero You're right I meant program. Sorry about that. Mar 22, 2011 at 17:34
• Generally, basic math concepts aren't great golf questions because J, APL, Maple, Mathematica and many others will have them built in. Also, be a bit more specific about input and output format, and provide example results - I can't tell if you mean 5 choose 2 or 2 choose 5 here. Mar 22, 2011 at 21:39
• @Jesse I got this challenge from another website that only allows major scripting languages, I'll remember this guideline in the future. I edited my question to try and make the challenge requirements more clear, let me know if its clear enough or not. Mar 22, 2011 at 23:36
• I'm new, so we're in much the same boat. However, don't summarize the results; they will just get outdated. Just vote and (if appropriate) accept answers. (Also, you'll make people unhappy if you ignore J and GolfScript answers without a reason.) Mar 23, 2011 at 4:09

## C 96

With I/O (which takes about 34 chars). Added a couple of newlines to make it readable.

main(a,b,c,d){scanf("%d,%d",&a,&b);
d=a-b;for(c=1;a>b;){c*=a--;}for(;d;)
{c/=d--;}printf("%d",c);}


Now if you'll excuse me, I have an ASCII n choose k rocket to catch.

    d;main(a
,         b
,           c
)  int        a
;   {scanf    (
(   "%d %d"   )
,   &a,  &b   )
;   d    =a   -
b   +     b   -
b   *     1   ;
a             -
a  ;for    (  c
=  1;a>   b   ;
)  {c=   c    *
(  (a-- )     )
;  }for(      b
=  b + 1      ;
d  ;    )     {
c  =     c    /
(  d--    )   ;
}           {
}           {
}         (
printf("%d",c)
)      ;       }
/*     *  *   * *\
* * X   * 8 * * |
|     *      *    \
*/    //       *  */


# APL, 3 bytes

⎕!⎕


Or for those whose browser doesn't render the above, in an ASCII rendering:

{Quad}!{Quad}

• To be completely conformant to the n,k input, you'd have to do !/⌽⎕. Dec 5, 2012 at 23:41

# R (11 Chars)

choose(n,r)

• You can save 5 bytes (and make it compliant with modern I/O rules) by removing (n,r) Feb 23, 2021 at 4:22

## GolfScript, 17 chars

This solution handles cases like k=0 or k=1 correctly.

~>.,,]{1\{)*}/}//


Factorial-like portion is based off a previous answer.

# GolfScript 21

~~)>.,,]{{)}%{*}*}%~/


Not particularly short, GolfScript lacks a real factorial function, however this has got to be the most wicked data manipulation that I have ever done, this calls for a stack trace:

"5,2" Data on the stack from input.
~ Eval command, note that , is an operator that turns a number into an array.
[0 1 2 3 4] 2
~ Binary not.
[0 1 2 3 4] -3
) Increment.
[0 1 2 3 4] -2
> Take end of array, -2 as parameter to get the last 2 elements.
[3 4]
. Duplicate element.
[3 4] [3 4]
, Array length.
[3 4] 2
, Turn number to array.
[3 4] [0 1]
] Create array.
[[3 4] [0 1]]
{{)}%{*}*} Block of code.
[[3 4] [0 1]] {{)}%{*}*}
% Execute block once for each element of the array. The following part only demonstrate the first loop.
[3 4]
{)}% Increment each array element.
[4 5]
{*} Block containing a multiply command.
[4 5] {*}
* "Fold" the array using the block command, that is in this case make the product of all elements.
20
After the big loop has finished it returns an array with the results.
[20 2]
~ Deconstruct the array.
20 2
/ Division.
10

## Ruby 1.9, 52 46 (42) characters

eval"A,B="+gets;i=0;p eval"(A-B+i+=1)/i*"*B+?1


If stderr is ignored:

eval"A,B=I="+gets;p eval"I/(A-I-=1)*"*B+?1


Ruby 1.8, 43 characters, no additional output to stderr:

eval"a,b=i="+gets;p eval"i/(a-i-=1)*"*b+"1"


Edits:

• (52 -> 48) Found a shorter way to parse the input
• (48 -> 46) Less looping, more eval.

## Python (56)

f=lambda n,k:k<1and 1or f(n-1,k-1)*n/k;print f(*input())


Ungolfed code and some explanation of a shortcut for calculating the binomial coefficient. (Note: There is some insight that I just haven't figured out in order to get down to the 39 char version; I don't think this approach will get you there.)

# Since choose(n,k) =
#
#     n!/((n-k)!k!)
#
#          [n(n-1)...(n-k+1)][(n-k)...(1)]
#        = -------------------------------
#            [(n-k)...(1)][k(k-1)...(1)]
#
# We can cancel the terms:
#
#     [(n-k)...(1)]
#
# as they appear both on top and bottom, leaving:
#
#    n (n-1)     (n-k+1)
#    - ----- ... -------
#    k (k-1)       (1)
#
# which we might write as:
#
#      choose(n,k) = 1,                      if k = 0
#                  = (n/k)*choose(n-1, k-1), otherwise
#
def choose(n,k):
if k < 1:
return 1
else:
return choose(n-1, k-1) * n/k

# input() evaluates the string it reads from stdin, so "5,2" becomes
# (5,2) with no further action required on our part.
#
# In the golfed version, we make use of the * unpacking operator,
# to unpack the tuple returned by input() directly into the arguments
# of f(), without the need for intermediate variables n, k at all.
#
n, k = input()

# This line is left as an exercise to the reader.
print choose(n, k)

• Could you provide an ungolfed example of your script? Mar 23, 2011 at 4:43
• Can we use * for parsing inputs of the form 4545 78? Mar 23, 2011 at 14:27
• Unfortunately, no, but it's not * that's the issue. 4545 78 isn't a valid Python expression, so input() will raise a SyntaxError. This trick depends entirely on the problem asking for x,y. If you had a function that read x y and returned a tuple, then you could use * with that just fine.
– user1011
Mar 23, 2011 at 16:52

## RPL (4)

(using built-in function)

COMB


## Windows PowerShell, 57

$a,$b=iex $input$p=1
for($a-=$b;$a-ge1){$p*=1+$b/$a--}$p  # J, 33 36 (":!~/".;._1}:toJ',',1!:1(3))1!:2(4)  35 characters are input, parsing and output. The other character, !, is n choose k. I don't have Windows around for testing this at the moment, but I believe it should work there. # Q, 32 chars {f:{1*/1.+(!)x};f[x]%f[y]*f x-y}  ## Perl 6 (55) my ($a,$b)=lines;$_=1;for 1..$a-$b {$_+=$_*$b/$^a};.say


## RPL (22)

(not using built-in COMB function)

→ n k 'n!/(k!*(n-k)!)'


## Q (50 45)

 f:{(prd 1.+til x)%((prd 1.+til y)*prd 1.+til x-y)}


You can shave a few characters off the above by removing redundant brackets and using 1*/ instead of prd.

f:{(1*/1.+til x)%(1*/1.+til y)*1*/1.+til x-y}


# Mathematica 12

Straightforward, built-in function.

n~Binomial~k


# Perl 6, 25 16 bytes

-9 bytes thanks to nwellnhof

+*o&combinations


Try it online!

Anonymous function that takes two numbers and returns an int. This uses the built-in combinations and converts the returned list to an int.

• 16 bytes Dec 8, 2018 at 10:45
• @nwellnhof Ah, I didn't realise combinations could take an number instead of a list
– Jo King
Dec 8, 2018 at 11:02

## PHP (71 79)

<?$a=fgetcsv(STDIN);$x=1;while($a-$i)$x=$x*($a-++$i+1)/$i;echo$x;


<?php $a=fgetcsv(STDIN);$x=1;while(++$i<=$a)$x=$x*($a-$i+1)/$i;echo$x?>


# Python (54)

f=lambda n,k:k<1or f(n-1,k-1)*n/k;print 1*f(*input())


Essentially the same as the Python one above, but I shave off four bytes by dropping the

and 1


from the function definition. However, this results in the function returning True instead of 1 if k=0, but this can be fixed by multiplying with 1 before printing, since 1*True=1, thus adding two bytes.

## J, 11 characters

!~/".1!:1[1


Takes input from the keyboard.

    !~/".1!:1[1
5,2
10


# Python 3.8 (pre-release), 21 bytes

import math
math.comb


Try it online!

f(_,0)=1
f(n,k)=n/k*f(n-1,k-1)


But, if input in the format x y is allowed instead of in the format x,y, it's 74 chars:

f[_,0]=1
f[n,k]=n/k*f[n-1,k-1]
main=interact$show.f.map read.take 2.words  ### Scala 54 val n,k=readInt ((k+1)to n product)/(1 to(n-k)product)  # Python (52)  f=lambda n,k:k<1or f(n-1,k-1)*n/k;print+f(*input())  Improved from the other two by using print+ to convert the result of f from boolean to int in case k==0. Still have no idea how to shrink it to 39, I wonder whether they are using lambda at all. (The OP only loosely specified the input & output method/format, so the following seems acceptable.) ## Sage Notebook (39 41 40) In the current cell, f=lambda n,k:k<1or f(n-1,k-1)*n/k;+f(*_)  where the input in the form n,k is entered & evaluated in the preceding cell. This simulates "command-line input" by assigning it to _ (similar to command-line arguments). ## Sage Notebook (42 44 43) Alternatively, using "in-source input" (with only the x= and newline characters adding to the score), e.g., x=5,2 f=lambda n,k:k<1or f(n-1,k-1)*n/k;+f(*x)  Both of these approaches are obviously spin-offs from earlier answers by others. # Tcl, 80 bytes proc C n\ k {proc f n {expr$n?($n)*\[f$n-1]:1}
expr [f $n]/([f$k]*[f $n-$k])}


Try it online!

• Very ungolfy approach, 165 bytes: tio.run/… Dec 4, 2018 at 23:23

# Javascript, 27 bytes

First my own 35-byte solutions:

f=(n,k)=>n-k&&k?f(--n,k)+f(n,k-1):1


Or, alternatively,

f=(n,k,i=k)=>i?f(n-1,k-1,i-1)*n/k:1


The first working recursively, with the simple (n,k) = (n-1,k) + (n-1,k-1) rule. The second using that (n,k) = (n-1,k-1) * n/k.

EDIT

I just noticed the solution by Arnould in a duplicate of this:

f=(n,k)=>k?n*f(n-1,k-1)/k:1


Which is a whopping 8 bytes less (27 bytes)

# TI-BASIC, 16 chars (8 bytes)

Ans(1) nCr Ans(2


Input is a list of length 2 in Ans.
Output is the result of the formula defined here.

If the above solution doesn't suffice, then the following 35 chars (24 bytes) solution also works:

Ans(2)!⁻¹(Ans(1)-Ans(2))!⁻¹Ans(1)!


Note: TI-BASIC is a tokenized language. Character count does not equal byte count.

# 05AB1E, 1 (or 5?) bytes

c


Builtins ftw ¯\_(ツ)_/¯

Takes two loose inputs, $$\k\$$ as first and $$\n\$$ as second.

Try it online.

If the "n,k" format is mandatory, it would be 5 bytes instead:

',¡c


Try it online.

This will split the (implicit) input on the ","; pop and push both values separated to the stack; and than uses the a choose b builtin.

# Excel, 13 chars

=COMBIN(A1,B1


A1 is n, B1 is r. Surprised this wasn't restricted, honestly.

# CJam, 19 bytes

l~\_m!@@\_m!@@-m!*/
`

Try it online!