
-----------------------------------
Viper
Thu Nov 10, 2005 1:57 pm

LowestCommonMultiple
-----------------------------------
**Recursion must be used** 
The LCM (lowest common multiple ) of two non-zero integers can be found using the formula: lcm(num1, num2) = num1*num2/ gcd(num1, num2) where:    gcd(num1, num2) is the greatest common divisor of num1 and num2. here's the trick.  the program should employ a recursive approach to finding the gcd of two number.  The recursive definition is given by:
if num2 divides evenly into num1 then 
gcd= num2

could someone re-write tht in C++ for me plz  :D 

here it is in turing
var lcm, gcd : real := 0
var num1, num2 : int := 0
%------------------------------------------------------------------------------------------
proc starter
    put "Enter the numbers you wish to find the 'LCM' for"
    put "***********Hit enter after each number**********"
    get num1
    get num2
end starter
%------------------------------------------------------------------------------------------
proc GCD (num1, num2 : real)
    if num1 mod num2 = 0 then
        gcd := num2
    else
        GCD (num2, num1 mod num2)
    end if
end GCD
%------------------------------------------------------------------------------------------
proc LCM (gcd, num1, num2 : real)
    lcm := num1 * num2 / gcd
    put "The 'LCM' for", " ", num1, " ", "&", " ", num2, " ", "is", " ", lcm
    put "The 'GCD' for", " ", num1, " ", "&", " ", num2, " ", "is", " ", gcd
end LCM

%MAIN-----------------------------------
starter
GCD (num1, num2)
LCM (gcd, num1, num2)

-----------------------------------
[Gandalf]
Thu Nov 10, 2005 4:17 pm


-----------------------------------
No.  Try it yourself first.

-----------------------------------
Geminias
Fri Nov 11, 2005 6:12 pm

hey
-----------------------------------
here you go :P



syntax = c++

#include 

void intro ();
float module ();
float lcm (float GCD);
void intro ()
{
     int num1 = 0, num2 = 0, N1 = 0, N2 = 0;
     int *pNum1 =  num1;
     int *pNum2 =  num2;
     int *pN1 =  N1;
     int *pN1 =  N2;
     
     
     std::cout  num2 then
        if num1 mod num2 = 0 then
           num2
        else
           gcd (num1 mod num2) num2
     else
        gcd num2 num1;;
val gcd : int -> int -> int = 
# gcd 12 8;;
- : int = 4
# gcd 2336 1314;;
- : int = 146
# let lcm num1 num2 = num1 * num2 / gcd num1 num2;;
val lcm : int -> int -> int = 
# lcm 57 12;;
- : int = 228
# 57 * 12;;
- : int = 684

You may also wish to hit Google.  Never know what you'll [url=http://www.topcoder.com/index?t=features&c=feat_010505]find.

-----------------------------------
Geminias
Sat Nov 12, 2005 1:35 pm


-----------------------------------
i fixed the logic so it will return 1 as both lcm and gcm in the case that there are no other common multiples.  

hey wtd, how do you get all that pretty color in your code lol

-----------------------------------
Andy
Sat Nov 12, 2005 1:50 pm


-----------------------------------
use code tags instead of [code ] use [sytax="cpp" ]

-----------------------------------
Geminias
Sat Nov 12, 2005 3:16 pm

Re: heyho
-----------------------------------
hey again, yeah i dropped out the idea of pointers and functions for this.  
it finds the lowest common multiple and the highest common multiple (LCM) (GCM) but it excludes 1.  Because 1 divides into everything.
in the case that only one number besides 1 is common to both numbers, then LCM = GCM.  

Viper, your recursion technique did not work.  It does not find the LCM and GCM.  

try this....  (note: if one of the numbers is prime the program crashes.)  

syntax = c++
#include 

int main ()
{
    unsigned long int num1, num2, gcm,lcm;
    std::cout = gcm ; i--)
  {
        if (num2 % i == 0 && num1 % i == 0)
     {   gcm = i;  
     }
     else if (i = 1)
    {   
        gcm = 1
     }

  }
     for (int i = 2; i 