Last active
April 17, 2018 21:59
-
-
Save timmyRS/b7614e2b3e94712381997be3016b6b83 to your computer and use it in GitHub Desktop.
An efficient PHP script using GMP to calculate factorials.
This file contains hidden or 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 | |
if(!isset($argv[1])) | |
{ | |
die("Syntax: php fac.php <num>\n"); | |
} | |
$start_time = microtime(true); | |
$fac = $one = gmp_init("1"); | |
$i = gmp_init("1"); | |
$limit = gmp_init($argv[1]); | |
do | |
{ | |
$i = gmp_add($i, $one); | |
$fac = gmp_mul($fac, $i); | |
} | |
while(gmp_cmp($i, $limit) != 0); | |
echo gmp_strval($limit)."! = ".gmp_strval($fac)."\n"; | |
echo "Calculated in ".(microtime(true) - $start_time)." seconds.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
amazing