Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LCM - Please help

Status
Not open for further replies.

nappaji

Programmer
Mar 21, 2001
76
US
Does any body know how to solve this problem???

Given two integer x and y, find a third integer z, such that
z > y and lcm(x,z) < lcm(x,y)

where LCM = least common multiple.

Thanks
 
You can always do this using brute force. Start with z = y+1 calculate the lcm(z,x), compare it to lcm(x,y) if its less then you have a z if not keep checking for z < lcm(x,y).

jaa
 
int z;
for(z=y+1;lcm(x,z)>=lcm(x,y);z++);
printf(&quot;%d&quot;,z);

try this code. hope u have the lcm function.

luv
Karthik.

LOL A ship is safe in the harbour, but that's not what it is meant for!!! LOL
 
int z;
for(z=y+1;lcm(x,z)>=lcm(x,y);z++);
printf(&quot;%d&quot;,z);

try this code. the for loop will search for the correct z and quit only when it finds. hope u have the lcm function.

luv
Karthik.

LOL A ship is safe in the harbour, but that's not what it is meant for!!! LOL
 
You will need to check that y is not a multiple of x first before starting the for loop, otherwise it will loop forever.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top