... preferably in Java. Here is what I have:
//x choose y
public static double choose(int x, int y) {
if (y < 0 || y > x) return 0;
if (y == 0 || y == x) return 1;
double answer = 1;
for (int i = x-y+1; i <= x; i++) {
answer = answer * i;
}
for (int j = y; j > 1; j--) {
answer = answer / j;
}
return answer;
}
I'm wondering if there's a better way of doing this?
I coded this in C#, but I tried to make it as applicable as possible to Java.
Derived from some of these sources, plus a couple small things from me.
Code:
for
use this (Pseudocode)
You could do something like this in O(k):
EDIT If
x
andy
are large, you will overflow more slowly (i.e., be safe for larger values of x & y) if you divide your answer as you go along:This version does not require
BigInteger
or floating-point arithmetic and works without overflow errors for alln
less than 62. 62 over 28 is the first pair to result in an overflow.The following test proves that this is true:
The numbers you are dealing with will become quite large and will quickly exceed the precision of
double
values, giving you unexpectedly wrong results. For this reason you may want to consider an arbitrary-precision solution such as usingjava.math.BigInteger
, which will not suffer from this problem.What you've got looks pretty clear to me, to be honest. Admittedly I'd put the return statements in braces as that's the convention I follow, but apart from that it looks about as good as it gets.
I think I'd probably reverse the order of the second loop, so that both loops are ascending.
As Greg says, if you need to get accurate answers for large numbers, you should consider alternative data types. Given that the result should always be an integer, you might want to pick
BigInteger
(despite all the divisions, the result will always be an integer):