For small numbers you can simply do:
array_product(range($numero, 1));
If you want 8!
range()
will create [8,7,6,5,4,3,2,1]
and array_product
will multiply all of them, resulting in 40320.
Try this.
Creating a class for this I believe is unnecessary, however:
class Fatorial
{
function calcular($numero)
{
return array_product(range($numero, 1));
}
}
Logo:
$resultado = 1;
if (isset($_GET['fat']) && ctype_digit($_GET['fat']) && $_GET['fat'] > 0) {
$fatorial = new Fatorial();
$resultado = $fatorial->calcular($_GET['fat']);
}
Try this.
If you need large numbers use bcmath , since your result is by string.