This article illustrates the GCD common algorithm for PHP to compute two integers. Share to everyone for your reference. Specifically as follows:
Copy Code code as follows:
<?php
Timing, returning seconds
function Microtime_float ()
{
List ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec);
}
//////////////////////////////////////////
Euclidean algorithm
function Ojld ($m, $n) {
if ($m ==0 && $n = 0) {
return false;
}
if ($n = = 0) {
return $m;
}
while ($n!= 0) {
$r = $m% $n;
$m = $n;
$n = $r;
}
return $m;
}
//////////////////////////////////////////
Based on the definition of GCD
function Basedefine ($m, $n) {
if ($m ==0 && $n = 0) {
return false;
}
$min = min ($m, $n);
while ($min >= 1) {
if ($m% $min = = 0) {
if ($n% $min ==0) {
return $min;
}
}
$min-= 1;
}
return $min;
}
////////////////////////////////////////////
Computational methods in middle school mathematics
function Baseschool ($m, $n) {
$MP = GetList ($m); All prime numbers less than $m
$NP = GetList ($n); All prime numbers less than $n
$MZ = Array (); Preserve the $m of the mass factor
$NZ = Array (); Preserve the $n of the mass factor
$MT = $m;
$nt = $n;
M all decomposition
Iterate through all the prime numbers of M, and when divisible by M, continue the next division, knowing that you cannot be divided and then you can divide by M.
Prime number, until all occurrences of the product of the prime number equal to M are stopped
foreach ($mp as $v) {
while ($mt% $v = = 0) {
$MZ [] = $v;
$MT = $MT/$v;
}
$c = 1;
foreach ($mz as $v) {
$c *= $v;
if ($c = = $m) {
Break 2;
}
}
}
n All decomposition
foreach ($np as $v) {
while ($nt% $v = = 0) {
$NZ [] = $v;
$nt = $nt/$v;
}
$c = 1;
foreach ($NZ as $v) {
$c *= $v;
if ($c = = $n) {
Break 2;
}
}
}
Common divisor
$JJ = Array_intersect ($mz, $NZ); Take intersection
$gys = Array ();
Take out the factor with the least number of occurrences in both numbers and remove the excess.
$c = 1; Number of times a record number appears
$p = 0; Record the last occurrence of the number
Sort ($JJ);
foreach ($jj as $key => $v) {
if ($v = = $p) {
$c + +;
}
ElseIf ($p!= 0) {
$c = 1;
}
$p = $v;
$MK = Array_keys ($mz, $v);
$nk = Array_keys ($NZ, $v);
$k = (count ($MK) > count ($nk)? Count ($nk): Count ($MK);
if ($c > $k) {
Unset ($JJ [$key]);
}
}
$count = 1;
foreach ($jj as $value) {
$count *= $value;
}
return $count;
}
Sequence of consecutive prime numbers for a given integer greater than or equal to 2
Eratosthenes Screening Method
function GetList ($num) {
$a = array ();
$a = array ();
for ($i = 2; $i <= $num; $i + +) {
$a [$i] = $i;
}
for ($i = 2; $i <= floor (sqrt ($num)); $i + +) {
if ($a [$i]!= 0) {
$j = $i * $i;
while ($j <= $num) {
$a [$j] = 0;
$j = $j + $i;
}
}
}
$p = 0;
for ($i = 2; $i <= $num; $i + +) {
if ($a [$i]!= 0) {
$L [$p] = $a [$i];
$p + +;
}
}
return $L;
}
/////////////////////////////////////
Test
$time _start = Microtime_float ();
Echo OJLD (60, 24); 0.0000450611 seconds
Echo Basedefine (60, 24); 0.0000557899 seconds
Echo Baseschool (60, 24); 0.0003471375 seconds
$time _end = Microtime_float ();
$time = $time _end-$time _start;
Echo ' <br> '. sprintf ('%1.10f ', $time). ' Seconds ';
I hope this article will help you with your PHP program design.