-5

I was curious as to what it would take to mathematically solve lotto. Just For Fun.

So I decided to find out exactly how many tickets I would need to buy, if I wanted to win the next Week's Lotto.

MY GOAL IS TO CREATE EVERY SINGLE TICKET POSSIBLE

I wrote a small program, which can generate every possible Lotto Ticket. Works fine, but it has two problems I can't figure out how to solve:

  1. Every time i work with larger values or large ranges Eg 6*49 the App FREEZES for hours calculating, in the end the App crashes. But if I try to calculate small values and ranges eg all possible 3*5 LOTTO tickets THE PROGRAM Works fine No Crash (3*5== means .... Amount of numbers*The Biggest Number)

  2. Problem two is: I do not know how to make the program stop. I have some End-results but I cannot figure out the equation to get to the end results or make the (WHILE LOOP STOP) program stop working. At the moment I just set it so that if it makes a million duplicate tickets. There are no more possible tickets to make, Exit Now.

The results i get from the experiment are :

3*3=1                   

3*4=4 

3*5=10 ( ie 10 maximum possible tickets)

3*6=20

3*7=35

3*8=56

Or If i work With 4s

4*4=1

4*5=5

4*6=15

4*7=35

4*8=70

or if i use 6s

6*6=1

6*7=7

6*8=28

6*9=84

There is website that can easily calculate it but i have no idea what the equation is:

http://www.lotterynumberspro.com/lottery-odds-calculator.php

My code so far

 //******************************Start Values*******************************************************************************

    NSMutableArray*starArray=[[NSMutableArray alloc]initWithCapacity:Rangesize];
    NSMutableSet *aSet;
    int x;
    for (x=1; x<=Rangesize; x++) {[starArray addObject:@(x)] ;}

    NSMutableArray*allStartValues=[[NSMutableArray alloc]initWithArray:starArray];

    NSLog(@"First ticket = %@",[allStartValues componentsJoinedByString:@"_"]);


    //***************************Master Array with one object*****************
    NSMutableArray*masterArray=[[NSMutableArray alloc]initWithObjects:allStartValues, nil];

    NSLog(@"The masterArray initialized  = %@",[masterArray componentsJoinedByString:@"_"]);





    //****************************************Search Master for instances of new ticket*************
    int count=0;
    NSMutableString *string = [NSMutableString string];

    while ([masterArray containsObject:allStartValues]&& count<=100000) {


        NSMutableSet *aSet2=[[NSMutableSet alloc]initWithCapacity:Rangesize];

        while(([aSet2 count]!=Rangesize))
        {
            int Randnum = (arc4random() % BnSize)+1;
            [aSet2 addObject:[NSNumber numberWithInt:Randnum]];
        }

        NSArray *arrayOfUniqueRandomNumbers = [aSet2 allObjects];
        NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
        NSArray*new=[arrayOfUniqueRandomNumbers sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];





        for(NSArray*EachElement in masterArray)
        {

            if ([masterArray containsObject:new]) {

                count++;
                break;
            }

            else {
 NSLog(@"ticket %@  Pass to add to Master",[new componentsJoinedByString:@"_"]);
                [masterArray addObject:new];
              //  NSLog(@"The masterArray updated  = %@",[masterArray componentsJoinedByString:@"_"]);
                break;
            }
        }       
    }

    int c=1;
    for (NSArray*element in masterArray) {

        [string appendString:[NSString stringWithFormat:@"Ticket (%i) is >> %@  \n",c ,[element componentsJoinedByString:@"_"]]];          

        _Scroll.text=string;
        c++;
        //NSLog(@"array size = %i",[masterArray count]);

    }
    }

@end

ANYONE KNOW HOW TO MAKE THIS PROGRAM BETTER??

rmaddy
  • 314,917
  • 42
  • 532
  • 579
BrianBeaven
  • 173
  • 1
  • 2
  • 11
  • There's a lot of ways to make something 'better'. Can you be more specific? –  Aug 10 '13 at 03:56
  • There's no equation, and the number of tickets you need to buy to be certain of winning is the same as the number of possible combinations. The exact number depends on the particular lottery, but it's likely to be in the hundreds of millions. That takes a fair amount of time and space to compute. – jscs Aug 10 '13 at 04:03
  • The program keeps freezing wen i use large numbers...my best guess is its a memory problem...My Pc is only 2gb RAM and every time i run the program My memory drops down to 12mb then the App crashes...small numbers work no problem – BrianBeaven Aug 10 '13 at 04:15

1 Answers1

4

If I understand your question correctly, you are trying to compute the number of ways to choose k different items out of n. That number is given by the "Binomial coefficient" C(n, k)

For example, the number of possibilities to choose 4 different numbers out of 1, ..., 7 is C(7, 4) = 35.

The binomial coefficient can be computed without actually creating all possible combinations as

          n  * (n-1) * (n-2) * ... * (n-k+1)
C(n, k) = ----------------------------------
          1  *   2   *   3   * ... *    k

ADDED: (In response to your comment) It is not necessary to compute factorials in order to compute C(n, k). In other words, one would normally not use the formula C(n, k) = n! / (k! * (n-k)!) because the factorials get quite large.

Instead you use the above expression in the form

          n    (n-1)   (n-2)          (n-k+1)
C(n, k) = -  * ----- * -----  * ... * -------
          1      2       3               k

A simple implementation could look like this:

long long int choose(int n, int k)
{
    if (k < 0 || k > n)
        return 0;

    long long int result = 1;
    // Use the fact that C(n, k) == C(n, n-k) to reduce k:
    if (k > n - k)
        k = n - k;
    for (int i = 1; i <= k; i++) {
        result = (result * (n+1-i)) / i;
    }

    return result;
}

Of course this can also overflow if the numbers get too large. But (since long long int has at least 64 bit) this is sufficient to compute all binomial coefficients up to n = 60.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • This is quite correct in that it will yield the _number_ of tickets. I thought the OP was asking for an equation for the tickets themselves. – jscs Aug 10 '13 at 18:15
  • @JoshCaswell: The question is not clear without ambiguity, but I decided to offer this solution because OP said *"... find out exactly how many tickets I would need to buy"*. That is also what the linked-to "Lottery Calculator" computes. You are of course right that actually creating all the combinations would take a lot of time. - So I am waiting for some feedback from the OP to see if that is what he needed! – Martin R Aug 10 '13 at 19:19
  • Agreed, this does answer that part. – jscs Aug 10 '13 at 19:21
  • Hey Martin i just finished building the function for the equation ...it works fine but i cannot calculate factorials for numbers greater than 20 or 21...Factorials are extremely large numbers 20plus digits....i tried long int doesnt work.....i tried long-long int not working either..im not sure what to do – BrianBeaven Aug 11 '13 at 05:20
  • @BrianBeaven: I have added more information and sample code. – Martin R Aug 11 '13 at 07:58
  • @BrianBeaven: You are welcome! - (Please note that you can "accept" helpful answers by clicking on the check mark. That marks the problem as solved, and gives some reputation points to you and to the author of the answer.) – Martin R Aug 11 '13 at 10:28
  • Hey Martin i just tried code....:) thanks works like a charm...That solved the Stop part of the program...and 60 should be fine by the way, most lotto's range (40-53). After i tested extreme values ....i ran instruments Time profiler..I tried a 6*40 problem....total maximum tickets == 3,900,000..but My Pc takes 4 minutes to generates 20,000 tickets...i would need A LOT! of time just to print 4 million tickets ...:( bummer – BrianBeaven Aug 11 '13 at 10:33
  • @BrianBeaven: Do you need only the *number* of tickets or do you actually want to *generate* all tickets? – Martin R Aug 11 '13 at 10:42
  • Generate All Possible tickets – BrianBeaven Aug 11 '13 at 10:55
  • @BrianBeaven: Did you google for "generate all combinations"? I think there are better algorithms than yours. – Martin R Aug 11 '13 at 11:01