I have six number sets. Each have two digit number which range between 01 to 49. How to create all possible combinations? And how to calculate total number of possible combinations?

e.g

  1. Number set:- A B C D E F
  2. Lower Limit:- 01 01 01 01 01 01
  3. Upper Limit:- 49 49 49 49 49 49

Result should be
01 01 01 01 01 01
01 02 03 04 05 06

And so on up to 49 49 49 49 49 49

Recommended Answers

All 17 Replies

how to calculate total number of possible combinations?

(49^6) = 49 * 49 * 49 * 49 * 49 * 49 = 13.841.287.201

How to create all possible combinations?

Simplest with 6 nested for loops, but that won't work. It will cause a stack overflow. Recursion will probably cause one too. You can generate a file with 49 lines first, then copy and extend those for every new number that has to be added. Another option is a while loop starting with 6 variables, all starting at 1, and incrementing from the back, calculating overflows until you're done.

How to generate like following with loops?

17 18 24 29 43 49
01 06 13 16 19 37
02 18 28 37 39 40
22 23 30 34 40 41

I'm working on "online lottery" project. so it is needed to store all results in mysql. Also it is needed to pick 1 number daily as winner. It will be stored in winners table and it will not picked again until all results of combinations are ended.

I will try to manage other things but at first big problem is to generate and store combinations.

I have posted some example numbers as different combination.

Member Avatar for diafol
$set = range(1,49);

shuffle and take out the first 6 entries maybe?   

Why do you need to store ALL possible combos?

If it's lottery then the number of results are less. since a single number cannot be drawn twice:

49 * 48 * 47 * 46 * 45 * 44

Start with creating entries for one number. Then every number needs to be appended with every other number, except for what's already in the list. Repeat 5 times.

@pritaeas can you help with writing code I'm little confused with "how to write loops".

@diafol
All possible combos needed to be viewed by client as he wants in admin section.

$set = range(1,49); i think this will work but how to write loops for All Number set:- A B C D E F

I think writing like $set_a = range(1,49); $set_b = range(1,49); $set_c = range(1,49); this will be helpful but little confused

somebody help please

Member Avatar for diafol

You want to store approx 14m lines? I really can't see what advantage that would be to anybody.

I was just showing how to create a randomish generator

$numbers = range(1,49);
shuffle($numbers);
$values = array_slice($numbers,0,6);
$keys = range('A','F');
$draw = array_combine($keys,$values);

To get approx 14m combos via nested loops or other means would take a while, but it could be done if you did 'pagination', where you just look at a limited sample of the whole. Say, groups of 100 or 500 or even a 1000. A group of a 1000 lines (combos) would still mean 14,000-ish pages.

I'll write up some code as soon as I can. I know I wrote this once, but haven't been able to find it yet. I prefer finding, over rewriting.

Here's the pseudo code:

create 6 tables, 1 for each step (a single string column is enough)

// step 1
for i in 1 .. 49
    insert i into table 1

// step 2
foreach r in table 1
    for i in 1 .. 49
        insert r + i into table 2

// step 3 .. 6
// same logic as step 2, progressing a table each step
Member Avatar for diafol

Won't that give duplicates values within a combo, e.g. 1,7,24,7,13,20 ?

//EDIT

Using the PEAR (Pyrus) package...

require_once 'Math/Combinatorics.php';

$combinatorics = new Math_Combinatorics;
$r = $combinatorics->combinations(range(1,49), 4);
echo '<pre>';
print_r(array_slice($r,0,100));
echo "</pre>";

It runs out of memory with 5 values. So you may need to store info incrementally?

Won't that give duplicates values within a combo, e.g. 1,7,24,7,13,20

Yes it will. According to the OP's example that's what he wants, although suggested otherwise later on. That's a small fix anyway, a simple check before inserting.

// step 2
foreach r in table 1
    for i in 1 .. 49
        if i not in r
            insert r + i into table 2
Member Avatar for diafol

Really interesting p. Will it create the whole dataset in one go - or will it run out of memory?

Am probably testing code tomorrow (while waiting for the car to get fixed). Am not sure if it will run out. Basically you could do this in a stored procedure and let MySQL handle the memory. Would have a better performance too, and you can run it without PHP timing out.

Member Avatar for diafol

Like it - didn't think of using DB to do it.

I tried this:

<?php
set_time_limit(0);

$pdo = new PDO('mysql:dbname=pritaea1_test;host=localhost', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// create tables
for ($i = 1; $i <= 6; $i++)
{
    $query = "CREATE TABLE lotto{$i} ( data VARCHAR(50) )";
    $statement = $pdo->prepare($query);
    $statement->execute();
}

// step 1 - fill table 1 with 1 .. 49
for ($i = 1; $i <= 49; $i++)
{
    $query = "INSERT INTO lotto1 VALUES ('{$i}')";
    $statement = $pdo->prepare($query);
    $statement->execute();
}

// step 2 -- fill table 2 with permutations of table 1 and 1 ..49
$query = "SELECT `data` FROM lotto1";
$statement = $pdo->prepare($query);
$statement->execute();
while ($obj = $statement->fetchObject())
{
    $values = explode(',', $obj->data);
    for ($i = 1; $i <= 49; $i++)
    {
        if (!in_array($i, $values))
        {
            $value = $obj->data . ',' . $i;
            $insert = "INSERT INTO lotto2 VALUES ('{$value}')";
            $stat = $pdo->prepare($insert);
            $stat->execute();
        }
    }
    unset($obj);
}

// step 3 -- fill table 3 with permutations of table 2 and 1 ..49
// < 1000 inserts per minute
$query = "SELECT `data` FROM lotto2";
$statement = $pdo->prepare($query);
$statement->execute();
while ($obj = $statement->fetchObject())
{
    $values = explode(',', $obj->data);
    for ($i = 1; $i <= 49; $i++)
    {
        if (!in_array($i, $values))
        {
            $value = $obj->data . ',' . $i;
            $insert = "INSERT INTO lotto3 VALUES ('{$value}')";
            $stat = $pdo->prepare($insert);
            $stat->execute();
        }
    }
    unset($obj);
}
?>

It's only been running step 3, which takes over 2 hours. The avarage inserts start at about a 1000 per minute, and slowly drops. I suggest you try to make a stored procedure of this.

Member Avatar for diafol

All possible combos needed to be viewed by client as he wants in admin section.

How many combos does he need to see? Again, what use would this be? There's an equal chance (one-off) any combo will be drawn. As I mentioned in a previous post, you may be better off producing 'pages' on the fly, rather than creating a static list which may take hours of processing. There's no way you'd display all 14m combos on a page at any one time anyway, is there?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.