Generate mathematical combinations with PHP array

Asked

Viewed 3,793 times

3

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

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

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

???? how to do it? 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...
  • I edited the answer with the complement.

1 answer

3


Combinations of 17 elements from 15 to 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 you exchange array() for [].

Link to generate online combination Online Calculator: Combinatorics. Generator of Combinations



The number of combinations is given by the formula

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

Making the bill without calculating machine we 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
  • Thanks @Leo Caracciolo ! Solved my problem.

  • Friend, just to complement... I have a database with all Lotofácil results. As you may know, 15 dozen are drawn in each draw. How would you compare the generated 15-dozens array with all database records on each FOR pass above? The idea is to avoid using an already drawn game. The select part in the comic book I know how to do. I used a spreadsheet with all results to make this comparison. I am now creating a PHP application to automate this task.

  • Your tip helped me because I play wheels with 17 tens. A game of 17 tens equals the combination of 136 games of 15 tens. The comparison with the BD has to be 15 to 15.

  • @Round, got it. I edited the answer with the compliment.

  • Sorry to relive this post, it’s just that I need something related to this. @Leo Caracciolo Do you know how I can generate these combinations with guaranteed hits? As this function is to create combinations for lottery games, there is the possibility to guarantee prize pool, if within the dozens chosen for the combination, have some numbers drawn. For example: 17 dozens chosen for combination, ensuring 11 points on at least 1 card.

  • @Brunooliveira, from what I saw the probability of hitting 11 points with 17 tens bet is 1 in 3, so there is no guarantee of hitting at least one card. https://www.sorteonline.com.br/lotofacil/probabilidades

  • @Leocaracciolo then... In fact it is not a bet with 17 tens... It is 17 tens chosen to generate betting cards with 15 tens. The guarantee of 11 hits is if the 15 drawn dozens are within the 17 dozens used to generate these cards. The code you posted, generates 136 cards of 15 tens. In the case of a function with this guarantee of 11 hits, would generate only 21 cards. Example: In this same site that sent the link, in the betting part of the lotofacil, if you select 17 dozens, it will offer you a closure with 17 dozens guaranteeing 14 hits.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.