20
\$\begingroup\$

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.

\$\endgroup\$
9
  • \$\begingroup\$ Function or full program? Your description says function that returns the correct result, but your example is a full program that prints it. \$\endgroup\$
    – Ventero
    Mar 22, 2011 at 16:38
  • \$\begingroup\$ @Ventero You're right I meant program. Sorry about that. \$\endgroup\$
    – backus
    Mar 22, 2011 at 17:34
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Mar 22, 2011 at 21:39
  • \$\begingroup\$ @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. \$\endgroup\$
    – backus
    Mar 22, 2011 at 23:36
  • \$\begingroup\$ 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.) \$\endgroup\$ Mar 23, 2011 at 4:09

36 Answers 36

1
2
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Excel, 13 chars

=COMBIN(A1,B1

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

\$\endgroup\$
0
\$\begingroup\$

CJam, 19 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Japt, 2 bytes

àV

Try it out here

\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 1 byte

c

Try it online!

Built-in.

Thunno 2, 9 bytes

w$w÷$$-w÷

Try it online!

Non-built-in.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.