the formula is actually very simple.

It is the product of the number of entries for each slot.

For example PA dialy number uses three balls each with the available numbers being 0 - 9.

So all combinations is 10 10 10 = 1000

Pennsylvania Cash 5 uses 1 - 39 and 5 balls are selected but a given number cannot be repeated so the total available combinations are 39 38 37 36 35 = 690,908,40

If what you want to know is how to calculate these totals dynamically for different games it is pretty simple.

Step 1) Calculate the range of numbers by taking (high - low) + 1.

Step 2) If you have a static range (i.e. PA daily number) proceed to step 3, else proceed to step 4.

Step 3)

<?php
$total = $range;
for($i=1;$i<$balls;$i++)
    $total *= $range;
?>

Proceed to step 5

Step 4)

<?php
$total = $range;
for($i=1;$i<$balls;$i++)
    $total = $total * ($range - $i);
?>

Step 5)

<?php
echo 'There are ' . $total . ' combinations for this lottery.';
?>

Two more exercised remain that are left to the reader.

Exercise 1: PA Daily number allows you to play boxed and straight. The above code calculates the number of straight possibilies (i.e. 123 != 321) however it does not calculate the number of boxed possibilities (i.e. 123 == 321).

Exercise 2: Some lotteries choose some balls from one pool and some balls from another pool. This means that their range schema does not fit into either of the above coded categories.

    wow, alot more simple than i thought. guess its good that i'm taking a/s level maths in college!! (definately need a brush up on my maths)

    anyway, i put together a selection of numbers with drawmacks code and it works fine.

    the english lottery works like any other lottery, choses 7 numbers from a range of 1-49, however the 7th number is a "bonus" ball.

    either way this is what i used in the end.

    <?php
    
    $ball = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
    21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
    42,43,44,45,46,47,48,49);
    
    $range = max($ball) - min($ball);
    $range++;
    $balls = 7;
    
    $total = $range; 
    for($i=1;$i<$balls;$i++) 
        $total = $total * ($range - $i);
    
    echo 'There are ' . $total . ' combinations for this lottery.'; 
    ?>
    

    thanks again!!

    Jon

      I think the total permutations are somewhere close to the 16million mark....

      (Remember hearing it somewhere about the UK lottery....)

        Are you sure it was that high? I thought it was 13 million...

          Well - whichever it was - I think it is less than the figure given by tuf-web's script... that is in the billions... (432938943360)

          Can't research from work though - darn internet police.....

          Ahh got it...

          7 balls (including the bonus ball) - does mean 49484746454443 - and does come out to 432,938,943,360 Much higher than I had thought....

          Oh well.... 432 billion..... seems like a shot in the dark to play that damn thing....

          But if there are that many permutations, how come someone usually wins the jackpot every time.

          Do we have to take into consideration that only six numbers are taken - as the bonus ball then doesn't count ?

          This would then make it about 100 billion - still making the probability of this very high.

          So does that mean then that there is a "lady luck" helping people win ?

            Originally posted by The Chancer
            So does that mean then that there is a "lady luck" helping people win ?

            It's hard to say without knowing how many games are played (you tend to hear more about the winners, so there's an observational bias). Here's a bit of a clue - where does the prize money come from?

              Originally posted by The Chancer
              So does that mean then that there is a "lady luck" helping people win ?

              Shhhh... you don't mention her by that name. Just "The Lady"...

                Originally posted by tuf-web.co.uk
                wow, alot more simple than i thought. guess its good that i'm taking a/s level maths in college!! (definately need a brush up on my maths)

                anyway, i put together a selection of numbers with drawmacks code and it works fine.

                the english lottery works like any other lottery, choses 7 numbers from a range of 1-49, however the 7th number is a "bonus" ball.

                either way this is what i used in the end.

                <?php
                
                $ball = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
                21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,
                42,43,44,45,46,47,48,49);
                
                $range = max($ball) - min($ball);
                $range++;
                $balls = 7;
                
                $total = $range; 
                for($i=1;$i<$balls;$i++) 
                    $total = $total * ($range - $i);
                
                echo 'There are ' . $total . ' combinations for this lottery.'; 
                ?>
                

                thanks again!!

                Jon [/B]

                wouldn't it just be easier to use:

                $balls = 7;
                $ballmax = 49;
                echo 'There are ' . pow($ballmax,$balls) . ' combinations for this lottery.';
                

                .... like I had previously mentioned...?... this yeilds combinations...

                max (max - 1..n) is permutations 🙂.... which is cleaner as:

                function factorial($num) {
                  for ($i = ($num - 1); $i > 0; $i--) {
                    $num *= $i;
                  }
                  return $num;
                }
                
                $balls = 7;
                $ballmax = 49;
                echo 'There are ' . (factorial($ballmax)/(factorial($ballmax - $balls)) . ' permutations for this lottery.';
                

                (or use the gmp_fact() function if gmp is installed/enabled)

                  but it's not a true factorial cause they're not choosing all 49 balls.

                    You could do this too:

                    <?php
                    
                    $ball = range(1,49);
                    $range = count($ball);
                    $balls = 7;
                    
                    $total = $range; 
                    for($i=1;$i<$balls;$i++) 
                        $total = $total * ($range - $i);
                    
                    echo 'There are ' . $total . ' combinations for this lottery.'; 
                    ?>

                      Originally posted by drawmack
                      but it's not a true factorial cause they're not choosing all 49 balls.

                      I know... thats why you divide by the factorial(max - number).

                        Originally posted by goldbug
                        I know... thats why you divide by the factorial(max - number).

                        That's a whole lot of extra processing on the cpu.

                        Let's see multiplication takes 5 (i think) clock cycles. You're doing 2 42 extra multiplications. That's 5 84 extra clock cycles. 420 extra clock cycles, everytime this script runs.

                          Originally posted by drawmack
                          That's a whole lot of extra processing on the cpu.

                          Let's see multiplication takes 5 (i think) clock cycles. You're doing 2 42 extra multiplications. That's 5 84 extra clock cycles. 420 extra clock cycles, everytime this script runs.

                          I never said it was faster. But i'd wager using the proper gmp_fact() function is speedy enough.

                          Speaking of which... why doesn't PHP have a built-in factorial function? It seems like a "core" enough math function to include.

                            Originally posted by Weedpacket
                            It's hard to say without knowing how many games are played (you tend to hear more about the winners, so there's an observational bias). Here's a bit of a clue - where does the prize money come from?

                            The prize money comes from the number of people who play.. £1 per play.

                            Average jackpot is about £8million but they do take for good causes as well as all the other smaller prizes.

                            Still - definately less than 100 Billion (1000 million) as that WOULD be a fantastic win....

                            So - "The Lady" MUST have something to do with it....

                              Originally posted by The Chancer
                              So - "The Lady" MUST have something to do with it....

                              Thats better 🙂

                                Of course, all this argument is assuming that the order in which the balls are drawn is relevant. But I'm presuming that 23,45,10,8,3,19 is just as good as 3,19,8,10,23,25. So you've got a huge overestimate; you're out by a factor equal to the number of possible permutations of six balls.

                                The number of possible permutations of six balls drawn from a set of forty nine is 49!/(49-6)!, but the number of possible combinations is only 49!/(6!(49-6)!).

                                function factorial($n)
                                {
                                	static $fact=array(1);
                                	if(isset($fact[$n])) return $fact[$n];
                                	return $fact[$n] = bcmul(factorial($n-1),$n);
                                }
                                echo bcdiv(factorial(49), bcmul(factorial(6), factorial(49-6)));
                                

                                  Originally posted by Weedpacket
                                  Of course, all this argument is assuming that the order in which the balls are drawn is relevant. But I'm presuming that 23,45,10,8,3,19 is just as good as 3,19,8,10,23,25. So you've got a huge overestimate; you're out by a factor equal to the number of possible permutations of six balls.

                                  I did mention that limitation on my initial post of my code. When the person asking the question used my code I assumed order was relevent as being from the states I have no idea how the english lotery works.

                                    7 numbers are chosen from the machine (6 normal and 1 a bonus, both chosen from the same machine)

                                    however you can only choose 6 numbers on your ticket.

                                    in my earlier code i forgot to change $balls=6; but even so that brings back

                                    10,068,347,520

                                    and as someone said before i dont think 10billion tickets are bought

                                      Originally posted by tuf-web-co.uk
                                      in my earlier code i forgot to change $balls=6; but even so that brings back

                                      10,068,347,520

                                      and as someone said before i dont think 10billion tickets are bought

                                      Really? I only get 13,983,816 (see above code, and piersk's recollection). Sound more plausible?

                                      Originally posted by drawmack
                                      I did mention that limitation on my initial post of my code.

                                      Yah, but no-one paid attention to it 🙂

                                        OK - so 13million was correct...

                                        I knew that the billion number was too great - just didn't know how to get the permutations of 6 balls...

                                        (I am still not sure - but there again, my maths isn't of the same high standard as some peoples.....😃 😃 )