Doubt in the code in probability calculation

Asked

Viewed 498 times

6

I’m making a calculator to estimate the probability of lottery hits.

I’m using this formula:

inserir a descrição da imagem aqui

Being:

  • to is the number of tens of the steering wheel (at Mega-sena, a = 60)
  • b is the number of dozens drawn (in Mega-Sena, b = 6)
  • k is the number of tens per steering wheel (on the Mega-sena if our steering wheel has 6 dozens, k = 6)
  • i total numbers that must be hit to win such a prize (for sena, i = 6, for the corner, i = 5 and for the court, i = 4)

Explained, let’s go to PHP. I made the following code:

<?php
$a = $_POST['a'];
$b = $_POST['b'];
$k = $_POST['k'];
$i = $_POST['i'];

// Realizando as subtrações
$c1 = $a - $k;
$c2 = $b - $i;

// Calculo para Ca-k,b-i
$calculo_1 = gmp_fact($c1) / (gmp_fact($c2) * gmp_fact(($c1 - $c2)));
// Calculo para Ck,i
$calculo_2 = gmp_fact($k) / (gmp_fact($i) * gmp_fact(($k - $i)));
// Calculo para Ca,b
$calculo_3 = gmp_fact($a) / (gmp_fact($k) * gmp_fact(($a - $k)));
// Multiplicação de Ca-k,b-i com Ck,i
$calculo_4 = $calculo_1 * $calculo_2;

// Simplificando
$calculo_final_cima = $calculo_4 / $calculo_4;
$calculo_final_baixo = $calculo_3 / $calculo_4;


echo "Probabilidade de acerto " . $calculo_final_cima . " em " . $calculo_final_baixo;
?>

The math checks out when $k receives the standard values of the lotteries (Mega-sena: 6, Lotofácil: 15, Quina: 5 [...]), however, if assigned $k = 7, getting:

// Exemplo da Mega Sena
$a = 60; // Números no total
$b = 6; // Dezenas que serão sorteadas
$k = 7; // Irei apostar com 7 números
$i = 6; // Acertos pretendidos

The expected result would be (according to the CEF): 1 in 7.151,980.

But I’m taking it back: 1 in 55,172,417.

Where I’m going wrong in the process?

  • 1

    I decided to change $calculo_3 = gmp_fact($a) / (gmp_fact($k) * gmp_fact(($a - $k))); for $calculo_3 = gmp_fact($a) / (gmp_fact($b) * gmp_fact(($a - $b)));.

  • Put as an answer.

1 answer

1

Every question deserves an answer.

By definition Cn,p (combinações simples de n elementos distintos tomados p a p) is equal to

    n!
_________
 p!(n-p)!

where the signal ! means factorial. (Fatorial de um número natural é o produto de todos os inteiros positivos menores ou iguais a ele).

To $calculo_1 and $calculo_2 the substitutions in the formula were made correctly, but it is not known why for the calculation of $calculo_3 involving the variables $a and $b you switched b for k, you were doing so well :)

    $a!
____________
 $b!($a-$b)!

therefore, in PHP, the correct to $calculo_3 is

                         gmp_fact($a)
              _____________________________________
              (gmp_fact($b) * gmp_fact(($a - $b)));
  • Perfect Leo, I gave a really traveled. After I refilled all the calculation in the same pen I went to realize the mistake. Thanks for the answer!

Browser other questions tagged

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