Frage

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??

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top