note 13343 modified in ref.math by aidan
| From: | aidan@php.net | Date: | Sat, 26 Jun 2004 03:12:11 +0000 |
| Subject: | note 13343 modified in ref.math by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71707@lists.php.net to get a copy of this message | ||
I found that when dealing with tables, a 'least common multiple' function is sometimes
useful for abusing tablespan and the likes.
So here goes (you may choose to remove the first part of the gcd function if the function call is
well-behaved):
<?php
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));
}
?>
This may or may not be something to consider adding to the mathematical function library.
--was--
<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://php.net/manual/en/ref.math.php