LotteryChances

CSharp · Console

/Program.cs

using System;
					
public class Program
{
    private static double Factorial(int value) {
      double result = 1.0;

      for (int i = 2; i <= value; ++i)
        result *= i;

      return result;
    }

    static double Combinations(int x, int y) {
      return Factorial(x) / Factorial(y) / Factorial(x - y);
	}		

	// a - all possible numbers in a lottery, say 49
	// t - all taken numbers, say 6 (so we have 6 from 49)
	// c - category: 1 - all numbers win; 2 - all numbers win except 1; 3 - all numbers win except 2
    private static double Chances(int a, int t, int c) {
       return Combinations(t, t - c + 1) *
              Combinations(a - t, c - 1) /
              Combinations(a, t);
	}	
	
	public static void Main()
	{
        // from 40 number we take 5 and have 4 (category 2) winning ones
		Console.WriteLine(Chances(40, 5, 2));
	}
}
;