pl en

Least Common Multiple

For me, to remember, and for you with hope it will be useful I'm showing two simple procedures. First gives us greatest common divisor, the second - least common multiple.


// greatest common divisor
int GCD(int i1, int i2)
{
       if(i1 < i2){
               int temp = i2;
               i2 = i1;
               i1 = temp;
       }

       int r = i2;

       while(r!=0){
               i2 = i1;
               i1 = r;
               r = i2 % r;
       }
       return i1;
}

// least common multiple
int LCM(int i1, int i2)
{
       int d = GCD(i1,i2);
       return i1 * i2 / d;
}

Main page: akolacz.tarchomin.pl
css xhtml