Why is it that when I call this method:
public double Combinations(int n, int k)
{
return (double)(Factorial(n) / (Factorial(k) * Factorial(n - k)));
}
I get an output of 3 different integers. I get the correct integers but I want them to be divided, added and subtracted like the math says above and therefore get a single answer (according to the math above). Why doesn't it do that?? I'm calling it from a different method :
public static void Main()
{
MegaMillionsOdds megaMillions = new MegaMillionsOdds();
megaMillions.Combinations(5,4);
}
How can I do math inside methods in C#?
You're doing integer division and therefore losing accuracy (1/3=0). You need to cast the output of the Factorial function to a double, and then do the division.
double a = Factorial(n);
double b = Factorial(k) * Factorial(n - k);
return a / b;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment