#!/usr/bin/env python #-------------------------------------------------------------------- # megawin2.py # # This program will compute the probability that a Califormia # MegaMillions lottery ticket matches all six winning numbers # by using a multi-part rule to obtain binomial coefficients. # # execute using: $ python megawin2.py # # programmer: ALLAN CRUSE # written on: 30 JAN 2011 #-------------------------------------------------------------------- def binomial( n, k ): if ( n < 0 ) or ( k < 0 ) or (n < k ): return 0 else: if ( n == 0 ) or ( k == 0 ) or ( k == n ): return 1 else: num = 1 den = 1 for j in range( k ): num *= (n - j) # will be positive den *= (k - j) # will be positive return num / den print print "Computing the number of different California", print "MegaMillions lottery tickets" print m = binomial( 56, 5 ) print "Number of ways to choose 5 things from 56 things is %d " % m n = binomial( 46, 1 ) print "Number of ways to choose 1 thing from 46 things is %d " % n s = m * n print "So the number of MegaMillions tickets is the product", print "(i.e., %d) " % s print f = 1.0/(1.0 * s) print "The probability your ticket matches all six winning numbers", print "is %1.10f " % f print