11
This is a rather complex but very interesting maths subject (known as "covering problem"),
And I'd like your help for implementing it.
Imagine a lottery game, where each ticket must choose 5 random numbers in a set of 50 numbers (from 1 to 50).
It's quite easy to know the probability of a winning ticket, or the probability to have 1, 2, 3 or 4 good numbers.
It's also quite easy to "generate" all the tickets that have 1, 2, 3, 4 good numbers.
My question (and code challenge) is related to this, but slightly different:
I want to buy some lottery tickets (the fewest possible), such as at least one of my tickets has 3 good numbers.
Challenge
Your goal is to implement a generic solution (as a program or just a function), like this, in any language:
// Input: 3 prameters
min_lottery_tickets(total_numbers_to_choose_from, how_many_numbers_to_choose, how_many_good_numbers_i_want)
For the above example, one would just have to call:
min_lottery_tickets(50, 5, 3)
and the program will generate the smallest set of tickets to play to achieve this goal.
Example:
min_lottery_tickets(10, 5, 2)
would output 7 tickets, like those:
1 2 3 4 5
5 6 7 8 9
10 1 2 6 7
10 3 4 8 9
3 4 6 7 8
1 2 3 8 9
1 4 9 5 10
because such tickets are enough to cover any pair of numbers from 1 to 10.
Output
Text, one line per ticket, tabulations or spaces between numbers
who wins
The most efficient program wins (i.e. the program generating the fewest tickets for the above parameters):
min_lottery_tickets(50, 5, 3)
Thanks!
Related. – Peter Taylor – 2013-12-11T10:57:30.500
4This question needs various clarifications. Are you after a program, a function, or either? Does the output format matter? Do the numbers have to be indexed from 1, or could they be indexed from 0? And what's the objective winning condition? – Peter Taylor – 2013-12-11T10:58:26.380
Okay, so generating all tickets and printing it is the trivial case? Would this be considered an actual(albeit bad) answer? – Cruncher – 2013-12-11T15:24:14.743
Well, you can do it, but it'd obviously be the worst possible answer. My goal is to apply this algorithm in real life, and buy the least tickets possible :) – xem – 2013-12-11T15:56:24.450
3@xem this almost belongs on Math SE then. Where they will probably prove to you that the numbers aren't in your favour(though there does exist some jackpot number s.t it's worth buying tickets) – Cruncher – 2013-12-11T16:02:43.357
2What is a good number? – DavidC – 2013-12-11T22:34:28.150
... a winning number. A number that gets picked. – xem – 2013-12-12T09:21:17.670
2I'm pretty sure that it's provable that you will lose a lot of money if you actually go buy the tickets output by such a program. – Michael Hampton – 2013-12-15T21:15:45.257
I'm with @DavidCarraher - I'm still not seeing what is a good number? "A number that gets picked" doesn't seem to make sense, because you are after a prediction of the result and yet your definition of "good" cannot be determined until after the draw. Perhaps you mean "good" as in, "a number that has been drawn the most times, based on this table of the last x draws", in which case your problem is trivial (e.g. akin to
SELECT TOP 5 num FROM draw_history GROUP BY num ORDER BY COUNT(1) DESC
). – jimbobmcgee – 2014-01-29T17:53:20.590Sorry if I wasn't clear enough: the "government" picks 5 numbers out of 50. Those are the good numbers. The citizens play lottery, and on each lottery ticket, they chose 5 numbers, hoping that most of them are "good". So in this exercise, we try to play as few tickets as possible to be sure that at least one has 3 good numbers. – xem – 2014-01-31T12:39:48.870