Skip to content

Instantly share code, notes, and snippets.

@timmyRS
Last active April 17, 2018 21:59
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 timmyRS/b7614e2b3e94712381997be3016b6b83 to your computer and use it in GitHub Desktop.
Save timmyRS/b7614e2b3e94712381997be3016b6b83 to your computer and use it in GitHub Desktop.
An efficient PHP script using GMP to calculate factorials.
<?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";
@eddiejibson
Copy link

amazing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment