8 shorter
source | link

PHP

Short, 6058

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

Tests

0 -> 1

1 -> 1

5 -> 120

125 -> 1.8826771768889E+209

170 -> 7.257415615308E+306

171 -> INF

Executes in microseconds.

Ungolfed

<?php
$r = $i = $argv[1]; // Set $r and $i to Arg.
while($i > 1) // Calculate while $i bigger than 1
{
    $i--; // Decrement $i (so it's not infinite)
    $r = $r * $i; // Calculation the Factorial
 }
 echo ($r==0 ? 1: $r); // Output and make 0! = 1
 ?>

Slighty Longer, 8886

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

Improvements

  • Output with \n
  • Doesn't throw error if no arg defined

PHP

Short, 60

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

Tests

0 -> 1

1 -> 1

5 -> 120

125 -> 1.8826771768889E+209

170 -> 7.257415615308E+306

171 -> INF

Executes in microseconds.

Ungolfed

<?php
$r = $i = $argv[1]; // Set $r and $i to Arg.
while($i > 1) // Calculate while $i bigger than 1
{
    $i--; // Decrement $i (so it's not infinite)
    $r = $r * $i; // Calculation the Factorial
 }
 echo ($r==0 ? 1: $r); // Output and make 0! = 1
 ?>

Slighty Longer, 88

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

Improvements

  • Output with \n
  • Doesn't throw error if no arg defined

PHP

Short, 58

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

Tests

0 -> 1

1 -> 1

5 -> 120

125 -> 1.8826771768889E+209

170 -> 7.257415615308E+306

171 -> INF

Executes in microseconds.

Ungolfed

<?php
$r = $i = $argv[1]; // Set $r and $i to Arg.
while($i > 1) // Calculate while $i bigger than 1
{
    $i--; // Decrement $i (so it's not infinite)
    $r = $r * $i; // Calculation the Factorial
 }
 echo ($r==0 ? 1: $r); // Output and make 0! = 1
 ?>

Slighty Longer, 86

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

Improvements

  • Output with \n
  • Doesn't throw error if no arg defined
7 shorter
source | link

PHP

Short, 6160

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

Tests

0 -> 1

1 -> 1

5 -> 120

125 -> 1.8826771768889E+209

170 -> 7.257415615308E+306

171 -> INF

Executes in microseconds.

Ungolfed

<?php
$r = $i = $argv[1]; // Set $r and $i to Arg.
while($i > 1) // Calculate while $i bigger than 1
{
    $i--; // Decrement $i (so it's not infinite)
    $r = $r * $i; // Calculation the Factorial
 }
 echo ($r==0 ? 1: $r); // Output and make 0! = 1
 ?>

Slighty Longer, 8988

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

Improvements

  • Output with \n
  • Doesn't throw error if no arg defined

PHP

Short, 61

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

Tests

0 -> 1

1 -> 1

5 -> 120

125 -> 1.8826771768889E+209

170 -> 7.257415615308E+306

171 -> INF

Executes in microseconds.

Ungolfed

<?php
$r = $i = $argv[1]; // Set $r and $i to Arg.
while($i > 1) // Calculate while $i bigger than 1
{
    $i--; // Decrement $i (so it's not infinite)
    $r = $r * $i; // Calculation the Factorial
 }
 echo ($r==0 ? 1: $r); // Output and make 0! = 1
 ?>

Slighty Longer, 89

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

Improvements

  • Output with \n
  • Doesn't throw error if no arg defined

PHP

Short, 60

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

Tests

0 -> 1

1 -> 1

5 -> 120

125 -> 1.8826771768889E+209

170 -> 7.257415615308E+306

171 -> INF

Executes in microseconds.

Ungolfed

<?php
$r = $i = $argv[1]; // Set $r and $i to Arg.
while($i > 1) // Calculate while $i bigger than 1
{
    $i--; // Decrement $i (so it's not infinite)
    $r = $r * $i; // Calculation the Factorial
 }
 echo ($r==0 ? 1: $r); // Output and make 0! = 1
 ?>

Slighty Longer, 88

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

Improvements

  • Output with \n
  • Doesn't throw error if no arg defined
6 changed names
source | link

PHP, 55 chars

Short, 61

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

Tests

Example:0 -> 1

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

1 -> 1

Explained:5 -> 120

125 -> 1.8826771768889E+209

170 -> 7.257415615308E+306

171 -> INF

Executes in microseconds.

Ungolfed

<?php
$r //= Short$i Open= Tag
$r=$i=$argv[1];$argv[1]; // ReadSet $r and $i to Arg.
while($i>1$i > 1) // WhileCalculate while $i bigger than one1
{
    $i--; // Decrement $i (so it's not infinite)
    $r=$r*$i;$r = $r * $i; // Calculation of the resultFactorial
 }
 echo $r;($r==0 ? 1: $r); // Output and make 0! = 1
 ?>

List of known bugs:

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

This Snippet (99 chars) excludes these bugs:

Slighty Longer, 89

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

Improvements

  • Output with \n
  • Doesn't throw error if no arg defined

PHP, 55 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 (99 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";?>

PHP

Short, 61

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

Tests

0 -> 1

1 -> 1

5 -> 120

125 -> 1.8826771768889E+209

170 -> 7.257415615308E+306

171 -> INF

Executes in microseconds.

Ungolfed

<?php
$r = $i = $argv[1]; // Set $r and $i to Arg.
while($i > 1) // Calculate while $i bigger than 1
{
    $i--; // Decrement $i (so it's not infinite)
    $r = $r * $i; // Calculation the Factorial
 }
 echo ($r==0 ? 1: $r); // Output and make 0! = 1
 ?>

Slighty Longer, 89

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

Improvements

  • Output with \n
  • Doesn't throw error if no arg defined
5 shortened scripts
source | link
4 explained code
source | link
3 bug free solution
source | link
    Post Undeleted by timmyRS
2 no cheating
source | link
    Post Deleted by timmyRS
1
source | link