Doubt in code in probability calculation

6

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

I'm using this formula:

Being:

  • aisthenumberofdozensofthesteeringwheel(inMega-sena,a=60)
  • bisthenumberofdozensdrawn(inMega-sena,b=6)
  • kisthenumberofdozensperflyer(intheMega-senaifourflyerhas6tens,k=6)
  • itotalnumbersthatmustbematchedtowinsuchaprize(forthesine,i=6,forthequina,i=5andforthecourt,i=4)

Explained,let'sgotoPHP.Imadethefollowingcode:

<?php$a=$_POST['a'];$b=$_POST['b'];$k=$_POST['k'];$i=$_POST['i'];//Realizandoassubtrações$c1=$a-$k;$c2=$b-$i;//CalculoparaCa-k,b-i$calculo_1=gmp_fact($c1)/(gmp_fact($c2)*gmp_fact(($c1-$c2)));//CalculoparaCk,i$calculo_2=gmp_fact($k)/(gmp_fact($i)*gmp_fact(($k-$i)));//CalculoparaCa,b$calculo_3=gmp_fact($a)/(gmp_fact($k)*gmp_fact(($a-$k)));//MultiplicaçãodeCa-k,b-icomCk,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;
?>

Accounts hit when $k receives the default values of lotteries (Mega-sena: 6, Lotofácil: 15, Quina: 5 [...]), however, if you assign $k = 7 , >

// 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 CEF): 1 in 7,151,980.

But I'm taking as a return: 1 in 55,172,417.

Where am I going wrong in the process?

    
asked by anonymous 10.11.2017 / 21:08

1 answer

1

Every question deserves a response.

By definition C n, p (combinações simples de n elementos distintos tomados p a p) equals

    n!
_________
 p!(n-p)!

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

For $calculo_1 and $calculo_2 the substitutions in the formula were done correctly, but it is not known because for the calculation of $calculo_3 that involve the variables $a and $b you changed b by k , you were doing so well:)

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

So, in PHP, the correct one for $calculo_3 is

                         gmp_fact($a)
              _____________________________________
              (gmp_fact($b) * gmp_fact(($a - $b)));
    
20.11.2017 / 19:04