note 13343 added to ref.math.php
| From: | cornelius at skjoldhoej dot dk | Date: | Tue, 12 Jun 2001 18:03:39 +0000 |
| Subject: | note 13343 added to ref.math.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-14374@lists.php.net to get a copy of this message | ||
<P>I found that when dealing with tables, a 'least common multiple' function is
sometimes useful for abusing tablespan and the likes.</P>
<P>So here goes (you may choose to remove the first part of the gcd function if the function
call is well-behaved):</P>
function gcd(n, m) //greatest common divisor
{
n=abs(n); m=abs(m);
if (n==0 and m==0)
return 1; //avoid infinite recursion
if (n==m and n>=1)
return n;
return m<n?gcd(n-m,n):gcd(n,m-n);
}
function lcm(n, m) //least common multiple
{
return m*(n/gcd(n,m));
}
<P>This may or may not be something to consider adding to the mathematical function
library.</P>
http://www.php.net/manual/en/ref.math.php