Skip to content

Instantly share code, notes, and snippets.

@tiagodavi
Created November 17, 2011 20:19
Show Gist options
  • Save tiagodavi/1374392 to your computer and use it in GitHub Desktop.
Save tiagodavi/1374392 to your computer and use it in GitHub Desktop.
Função recursiva que calcula o fatorial de um número
<?php
//Função recursiva que calcula o fatorial de um número
function fatorial($numero){
if($numero <= 1){
return $numero;
}else{
return $numero * fatorial($numero - 1);
}
}
//4*3*2 = 24
echo fatorial(4);
@russo97
Copy link

russo97 commented Jun 5, 2017

Why not:

<?php
    function fatorial ($n) {
        return ($n) ? $n * fatorial($n - 1) : 1;
    };
    echo fatorial(4);
?>

@SlackPen
Copy link

SlackPen commented Nov 28, 2017

Sorry, this the right:

function fatorial ($n) {
    return  $n > 1 ? $n * fatorial($n - 1) : 1;
};
echo fatorial(4);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment