4 of 8 explained code

PHP, 56 chars

<? $r=$i=$argv[1];while($i>1){$i--;$r=$r*$i;} echo $r;?>

Example:

root@raspberrypi:~# php fact.php 5
120root@raspberrypi:~#

Explained:

<? // Short Open Tag
$r=$i=$argv[1]; // Read Arg
while($i>1) // While $i bigger than one
{
    $i--; // Decrement $i
    $r=$r*$i; // Calculation of the result
 }
 echo $r; // Output
 ?>

List of known bugs:

  • Output without \n
  • Throws error if $argv[1] is undefined
  • 0! = 0 ...

This Snippet (100 chars) excludes these bugs:

<? $r=$i=(isset($argv[1])?$argv[1]:0);if($i==0)$r=1;else while($i>1){$i--;$r=$r*$i;} echo $r."\n";?>