## Mega Millions

Friday, March 30, 2012

With the Mega Millions jackpot over $640 million, you might be asking yourself:

How do I use Factor to win the lottery???

You can find a good article on Forbes about calculating your odds of winning. We can also calculate it with Factor. The lottery works like this: you have to choose 5 “main” numbers (1 through 56) and then pick a “mega” number (1 through 46).

We can use the “n choose k” formula in
math.combinatorics
to compute the number of ways of picking “*5* unordered outcomes from
*56* numbers and then *1* of *46* possible mega numbers”:

```
IN: scratchpad 56 5 nCk 46 * .
175711536
```

Sure enough, thats 175,711,536 possibilities. So if you buy one random
lottery ticket, you have a *1 in 175,711,536* chance of winning, or a
0.00000000569114597006% chance. Not that great! What are the other odds?

Since the jackpot is so large, it’s got to be worth playing, right? In fact, if you take the total jackpot and the odds of each winning category, you can find the expected value of a ticket:

```
IN: scratchpad {
640000000/175711536
250000/3904701
10000/689065
150/15313
150/13781
7/306
10/844
3/141
2/75
} sum "$%.2f" printf
$3.82
```

Wow, $3.82 of expected value (not counting sharing the jackpot with someone who picked the same numbers, or the fact you can only win the highest prize you qualify for)! But, how do we pick our numbers… well, Factor to the rescue! Let’s randomly sample our 5 main numbers and then pick a random mega number and output the result:

```
IN: scratchpad : mega ( -- seq )
56 [1..b] 5 sample natural-sort
46 [1..b] random suffix ;
```

In the spirit of fiver, we can generate these numbers to play:

```
IN: scratchpad 5 [ mega ] replicate .
{
{ 2 8 25 30 43 29 }
{ 6 15 31 41 47 33 }
{ 11 20 26 29 33 15 }
{ 3 6 19 23 32 15 }
{ 16 25 31 44 50 24 }
}
```

And tonight we can check the winning numbers and see if it worked!