PHP greatest common divisor common algorithm for calculating two integers
<?php
Chronograph, return 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 greatest common divisor
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;
}
////////////////////////////////////////////
The calculation method of 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 (); Preserving the quality factor of $m
$NZ = Array (); Preserving the quality factor of $n
$MT = $m;
$nt = $n;
M all factorization
Traverse all the prime numbers of M, and when divisible by M, continue the next division, knowing that it cannot be divisible and then one can be divisible by M
of prime numbers, until all occurrences of prime numbers are equal to M when 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 factorization
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 factor
$JJ = Array_intersect ($mz, $NZ); Take intersection
$gys = Array ();
Remove the least number of factors in the two numbers and remove the excess.
$c = 1; Record number of occurrences
$p = 0; Record the last occurrence of a 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;
}
A sequence of consecutive prime numbers given integers greater than or equal to 2
Erato 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 ';
PHP greatest common divisor algorithm to take two integers