Algorithm for calculating the maximum common divisor of two integers
1. [PHP] code
= 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 less than $ m $ np = getList ($ n ); // all prime numbers less than $ n $ mz = array (); // save the prime factor of $ m $ nz = array (); // save the prime factor $ 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, know that the product cannot be divisible by m, and then take the next prime number that can be divisible by m until the product of all the prime numbers is equal to m to stop 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 factor 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 of the number $ 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 ;} // Obtain the continuous Prime number sequence of integers greater than or equal to 2 // function getList ($ num) {$ 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 secondsecho baseSchool (60, 24 ); // 0.0003471375 seconds $ time_end = microtime_float (); $ time = $ time_end-$ time_start; echo'
'. Sprintf (' % 1.10f', $ time). 'seconds ';