Created
September 27, 2013 15:50
-
-
Save carbontwelve/6730758 to your computer and use it in GitHub Desktop.
Generates factorial's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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