I am interested in calculating odds of winning UK Lottery. The format is that 6 numbers from 1-59 are drawn. I am interested only (at this stage) in the odds of winning the jackpot (matching six balls). As an aside, I'm interested in the odds for a total balls count of 49, and 59, to see the change in chance of winning.
The mathematical formula for calculating the odds is (where 49 is total balls, 6 is number drawn:
\$\text{Odds of winning} = \dfrac{49!}{6!*(49-6)!}\$
The main method of my code is to collect input from the user on parameters of the draw.
I have a class called DrawInfo to store information about the draw. I have a simple method to return the Factorial of a number.
I have a method to calculate the odds of winning the jackpot. This is currently all in the one class, as a small, simple app. I do appreciate that DrawInfo could live in its own class.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the total number of balls in the draw: ");
int totalBalls = int.Parse(Console.ReadLine());
Console.WriteLine("enter the number of balls drawn: ");
int ballsDrawn = int.Parse(Console.ReadLine());
DrawInfo di = new DrawInfo(totalBalls, ballsDrawn);
int totalWinOdds = FindJackpotWinningOdds(di);
Console.WriteLine(String.Format("the odds are 1/{0:n0}", totalWinOdds));
Console.ReadLine();
}
static int FindJackpotWinningOdds(DrawInfo di)
{
BigInteger totalBallsFactorialSum = Factorial(di.TotalBalls);
BigInteger ballsDrawnFactorialSum = Factorial(di.BallsDrawn);
BigInteger JackpotWinningOdds = 0;
JackpotWinningOdds = totalBallsFactorialSum / ((ballsDrawnFactorialSum * Factorial((di.TotalBalls - di.BallsDrawn))));
return (int)JackpotWinningOdds;
}
static BigInteger Factorial(BigInteger i)
{
if (i <= 1)
{
return 1;
}
return i * Factorial(i - 1);
}
}
public class DrawInfo
{
public int TotalBalls { get; set; }
public int BallsDrawn { get; set; }
public DrawInfo(int totalBalls, int ballsDrawn)
{
this.TotalBalls = totalBalls;
this.BallsDrawn = ballsDrawn;
}
}