Skip to content

Instantly share code, notes, and snippets.

@carbontwelve
Created September 27, 2013 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbontwelve/6730758 to your computer and use it in GitHub Desktop.
Save carbontwelve/6730758 to your computer and use it in GitHub Desktop.
Generates factorial's
<?php
$time_start = microtime(true);
$iterations = 20;
header('Content-Type: text/plain');
function convert($size)
{
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
function factorial( $n )
{
return accumulate( 1, $n );
}
function accumulate( $current, $n )
{
return $n == 0 ? $current : accumulate( $current * $n, $n - 1);
}
echo "Generating Factorals up to $iterations \n";
for ( $i = 1; $i <= $iterations; $i++ )
{
echo str_pad ( $i, 4 , ' ' ) . factorial( $i ) . "\n";
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Completed in $time seconds using ". convert(memory_get_usage(true)) . " memory\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment