PHP greatest common divisor algorithm to take two integers

Source: Internet
Author: User
Tags greatest common divisor

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.