This article mainly introduces common algorithms for calculating the maximum common approx of two integers in php. The example summarizes three common methods for finding the maximum common approx, for more information about how to use php to calculate the maximum common number of two integers, see the examples in this article. Share it with you for your reference. The details are as follows:
The code is as follows:
<? Php
// Time, 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;
}
//////////////////////////////////////// //
// Defined based on the maximum public approx.
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;
}
//////////////////////////////////////// ////
// Calculation method in middle school mathematics
Function baseSchool ($ m, $ n ){
$ Mp = getList ($ m); // all prime numbers smaller than $ m
$ Np = getList ($ n); // all prime numbers smaller than $ n
$ Mz = array (); // save the prime factor of $ m
$ Nz = array (); // save the prime factor of $ n
$ Mt = $ m;
$ Nt = $ n;
// All prime factors of m
// Traverse all prime numbers of m. When m can be divisible, continue the next division, knowing that it cannot be divisible and then the next one can be divisible by m
// The prime number until the product of all displayed prime numbers is equal to m.
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 prime factors
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;
}
}
}
// Public factor
$ Jj = array_intersect ($ mz, $ nz); // Obtain the intersection
$ Gys = array ();
// Remove the least frequent factor among the two.
$ C = 1; // record the number of occurrences
$ P = 0; // record the previous 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;
}
// Calculate the continuous Prime number sequence of given integers greater than or equal to 2
// The heratosini syntax
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'
'. Sprintf (' % 1.10f', $ time). 'seconds ';
I hope this article will help you with php programming.