11
0
Write a program that takes an input such as:
n,k
which then computes:
and then prints the result.
A numerical example:
Input:
5,2
Internal computation:
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.
The current lowest character counts by language are:
Perl: 35
Ruby: 36
Python: 39
PHP: 62
Function or full program? Your description says function that returns the correct result, but your example is a full program that prints it. – Ventero – 2011-03-22T16:38:42.707
@Ventero You're right I meant program. Sorry about that. – Backus – 2011-03-22T17:34:03.440
3Generally, 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. – Jesse Millikan – 2011-03-22T21:39:57.723
@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. – Backus – 2011-03-22T23:36:10.590
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.) – Jesse Millikan – 2011-03-23T04:09:46.800
@Jesse those aren't the answers from this page, they're the records from the other website and (as far as I know) the lowest possible counts. – Backus – 2011-03-23T04:48:27.760
@Jesse: Whether it's 5C2 or 2C5 is pretty easy to figure out, though, as you wouldn't normally want the latter ;-) – Joey – 2011-03-23T05:33:43.307
What about additional output to stderr, will that be ignored? – Ventero – 2011-03-23T06:40:52.180
Even this will work:
n,k=input();f=lambda x:x<2or x*f(x-1);print f(n)/(f(k)*f(n-k))
– Quixotic – 2011-03-23T14:24:47.550