[algogeeks] Re: Calculating C(n.r)

2014-04-08 Thread Don
Use the fact that a*b*c*d = exp(log a + log b + log c + log d) double sum = 0.0; double x; for(x = n-r+1.0; x = n; x += 1.0) sum += log(x); for(x = 2; x = r; x += 1.0) sum -= log(x); result = exp(sum); Don On Tuesday, April 8, 2014 2:06:47 PM UTC-4, kumar raja wrote: Hi all, I know

[algogeeks] Re: Calculating C(n.r)

2014-04-08 Thread Don
Note that my example is for C(n,r), but you can use the same method for your second formula. Just add up the logs of whatever you multiply in the numerator and subtract the logs of the factors of the denominator. Then the result is exp of the resulting sum. Don On Tuesday, April 8, 2014

[algogeeks] Re: Calculating C(n.r)

2014-04-08 Thread Dave
What you want to do is automate the process of cancelling common factors that you would do if you were calculating nCr by hand. Fill an array a[] with the numerator terms, n, n-1, n-2, ..., n-r+1, and a second array b[] with the denominator terms, 1, 2, 3, ..., r. Inside a double loop with j