Incorrect use of function or poorly implemented logic?

Asked

Viewed 68 times

2

I am developing an application for lotteries and wrote a function for unfolding tens. She gets a array() containing N tens and unfolds in all possible combinations.

For example, for the array:

$dezenas = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17');

I use the function gera_apostas_a():

/******************************
 * Função geradora "A"
 ******************************/
$resultado = array(); 
$combinacao = array();
// Quantas dezenas serão geradas por aposta
$dez_por_apostas = 16;
// Total de dezenas em $dezenas
$total_dez_escolhidas = count($dezenas);

function gera_apostas_a($inicio, $dez_por_apostas, $aposta, $total_dez_escolhidas) {
    global $resultado, $combinacao;

    if ($dez_por_apostas == 0) {
        array_push($resultado, $combinacao);
    } else {
        for ($i = $inicio; $i <= $total_dez_escolhidas - $dez_por_apostas; ++$i) {
            array_push($combinacao, $aposta[$i]);
            gera_apostas_a($i + 1, $dez_por_apostas - 1, $aposta, $total_dez_escolhidas);
            array_pop($combinacao);
        }
    }
}
gera_apostas_a(0, $dez_por_apostas, $dezenas, $total_dez_escolhidas);

// Só para deixar mais claro, apostas desdobradas estão armazenadas em $resultado
var_dump($resultado);

Will be generated 17 bets of 16 numbers, each. So far everything ok, working perfectly.

What I’m trying to do is, each previous game, is unfolded into 15 dozens, each and tried this way:

/******************************
 * Função geradora "B"
 ******************************/
$resultado_i = array(); 
$combinacao_i = array();
// Quantas dezenas serão geradas por aposta
$dez_por_apostas_i = 15;
// Total de dezenas por aposta que foi gerada em gera_apostas_a()
$total_dez_escolhidas_i = 16;

function gera_apostas_b($inicio_i, $dez_por_apostas_i, $aposta_i, $total_dez_escolhidas_i) {
    global $resultado_i, $combinacao_i;

    if ($dez_por_apostas_i == 0) {
        array_push($resultado_i, $combinacao_i);
    } else {
        for ($j = $inicio_i; $j <= $total_dez_escolhidas_i - $dez_por_apostas_i; ++$j) {
            array_push($combinacao_i, $aposta_i[$j]);
            gera_apostas_b($j + 1, $dez_por_apostas_i - 1, $aposta_i, $total_dez_escolhidas_i);
            array_pop($combinacao_i);
        }
    }
}

$ar_gerados_total = array();
foreach ($resultado as $rlt) {
    $ar_gerados = array();
    foreach ($rlt as $rlts) {
        array_push($ar_gerados, $rlts);
    }
    array_push($ar_gerados_total, $ar_gerados);
}

foreach($ar_gerados_total as $ar_apostas) {
    asort($ar_apostas);
    gera_apostas_b(0, 15, $ar_apostas, 16);

    foreach ($resultado_i as $ar_apostas) {
        foreach ($ar_apostas as $ar_dezenas) {
            echo sprintf("%02d", $ar_dezenas) . " ";
        }
        echo PHP_EOL;
    }
}

With that I’m getting a total of 2448 games of 15 tens but, of that amount, 2312 are coming repeated.

P.S.: 2448 - 2312 = 136, which is the total of a simple unfolding of 17 dozens with 15 numbers per bet.

Where I’m traveling here?

  • Your "B" generating function" is not working. Fix it so we can simulate.

No answers

Browser other questions tagged

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