The method of greatest common divisor and least common multiple implemented by PHP

Source: Internet
Author: User
Tags greatest common divisor php programming
This article mainly introduces PHP programming to find greatest common divisor and least common multiple methods, involving the mathematical calculation of PHP related arithmetic skills, the need for friends can refer to the next

Specific as follows:

Request greatest common divisor function max_pisor ($a, $b) {   $n = min ($a, $b);   for ($i = $n; $i >1; $i-)   {     if (is_int ($a/$i) &&is_int ($b/$i))     {     return $i;  If the echo $i is used here, then the output is 432; the difference between echo and return     }   }   return 1;} Ask least common multiple function  min_multiple ($a, $b) {  if ($b ==0)   //must consider divisor cannot be zero   {     return $b;   } else{   $m = max ($a, $b);   $n = min ($a, $b);   for ($i =2;; $i + +)   {     if (is_int ($m * $i/$n))     {       return $i;   }}} return $a * $b;} The greatest common divisor function max_pisor2 ($a, $b) {   if ($b ==0)   {     return $a;   }   else   {     return Max_pisor2 ($b, ($a% $b));}   } Add and subtract greatest common divisor function Max_pisor3 ($a, $b) {   if ($a = = $b)   {     return $a;   }   ElseIf ($a > $b)   {     $a = $a-$b;   }   else   {     $b = $b-$a;   }   Return Max_pisor3 ($a, $b);}

Related recommendations:

JS obtains least common multiple and the largest number of conventions

What is the maximum number of conventions for two integers?

2. Maximum number of conventions for M and N and least common multiple (maximum number of conventions : rotary-Rolling Division method)

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.