Generate mathematical combinations with array in PHP

1

GENERATE all possible combinations of 15 elements from an array with 17 items. This is a hypothetical situation. Should serve any combination n by p.

A total of 136 combinations. Using the Mathematical Formula for Combinations:

Cn,p
C17,15 = 17!/(15!*2!)

???? how to make? suggestions, examples?

//Código

//array com 17 dezenas

dezenas = ["2", "4", "5", "6", "7", "9", "10", "12", "15", "16", "18", "20", "21", "22", "23", "24", "25"];

//Gerar todas combinações possíveis de array com 15 dezenas

 array1 = ["2", "4", "5", "6", "7", "9", "10", "12", "15", "16", "18", "20", "21", "22", "23"];

 array2 = ["2", "4", "5", "6", "7", "9", "10", "12", "15", "16", "18", "20", "21", "22", "24"];

//etc... etc...
    
asked by anonymous 01.08.2017 / 02:21

1 answer

1

Combinations of 17 elements of 15 in 15 can be defined recursively using the following function:

$dezenas = array("2", "4", "5", "6", "7", "9", "10", "12", "15", "16", "18", "20", "21", "22", "23", "24", "25");

function combinacoesDe($k, $xs){
     if ($k === 0)
         return array(array());
     if (count($xs) === 0)
         return array();
     $x = $xs[0];
     $xs1 = array_slice($xs,1,count($xs)-1);
     $res1 = combinacoesDe($k-1,$xs1);
     for ($i = 0; $i < count($res1); $i++) {
         array_splice($res1[$i], 0, 0, $x);
     }
     $res2 = combinacoesDe($k,$xs1);
     return array_merge($res1, $res2);
}


print_r(combinacoesDe(15,$dezenas));

Result - ideone

  

From PHP 5.4 you can also use the contracted array syntax, which exchanges array () for [].

Link to generate combination online Online calculator: Combinatorics. Generator of combinations

Complementarily

As per the author's request in commenting on this response, I added a routine to compare the array of 17 tens, combined from 15 to 15, with all records in the database.

Functional Example

Table used in the example:

$dezenas=array("01", "03", "04", "08", "11", "12", "13", "14", "16", "17", "18", "19", "20", "21", "22", "23", "25");

function combinacoesDe($k, $xs){

        if ($k === 0)
            return array(array());
        if (count($xs) === 0)
            return array();
        $x = $xs[0];

        $xs1 = array_slice($xs,1,count($xs)-1);

        $res1 = combinacoesDe($k-1,$xs1);

        for ($i = 0; $i < count($res1); $i++) {
            array_splice($res1[$i], 0, 0, $x);
        }
        $res2 = combinacoesDe($k,$xs1);

        return array_merge($res1, $res2);

 }

//print_r(combinacoesDe(15,$dezenas));

$resultado = combinacoesDe(15,$dezenas);

$servername = "localhost";
$username = "USUARIO";
$password = "SENHA";
$dbname = "NOME_DB";

$conn = new mysqli($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

foreach($resultado as $key => $value)
{
    $linha = implode($value,',');

    $sql = "SELECT * FROM lotofacil where dezenas = '$linha'";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            echo "Combinação: ". $linha. " ::: Concurso: " . $row["concurso"]. " - Dezenas sorteadas: " . $row["dezenas"]. "<br>";
        }
    }
}

mysqli_close($conn);

The number of combinations is given by the formula

                 n!
      Cn,p = ___________     
             p! (n – p)!
  • n is the number of elements of a set
  • p is a natural number less than or equal to n, which represents the number of elements that will form the clusters.

Making the count without the calculator will have:)

                 17!
     C17,15 = ____________     
             15! (17 – 15)!


               (1*2*3*.......*15)*16*17     16*17
     C17,15 =  _________________________ = ______ = 8*17= 8*(10+7)=80+56=136
               (1*2*3*.......*15)*1*2        1*2
    
01.08.2017 / 05:01