Summary of common algorithms used to calculate the maximum common number of two integers in php

Source: Internet
Author: User
This article mainly introduces the common algorithms used to calculate the maximum common approx of two integers in php. The example summarizes the three common methods for finding the maximum common approx. It has some reference value. For more information, see

This article mainly introduces the common algorithms used to calculate the maximum common approx of two integers in php. The example summarizes the three common methods for finding the maximum common approx. It has some reference value. For more information, see

This example describes a common algorithm used by php to calculate the maximum common number of two integers. 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.

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.