Finding LCM & GCD in C
Two of my friends faced a problem with writing a program which finds the LCM(Lowest Common Multiple)/GCD(Greatest Common Divisor) of two positive integers, so I helped them out by writing two functions for each. I thought many others might be having the same problem.
So here are the functions: For LCM: Code:
/* a & b are the numbers whose LCM is to be found */ Code:
/* a & b are the numbers whose GCD is to be found. 
Re: Finding LCM & GCD in C
For LCM
Code:
if(n%a == 0 && n%b == 0) Code:
if(a%n == 0 && b%n == 0) Code:
for(n=1;;n++) Code:
for(n=1;a%n == 0 && b%n == 0;n++); Code:
int gcd(int a,int b) 
Re: Finding LCM & GCD in C
Shabbir,
Least Common Multiple(L.C.M.) of 'a' and 'b' is the smallest number 'n' which is both perfectly divisible by 'a' as well as 'b'; i.e. n%a == 0 && n%b == 0. 
Re: Finding LCM & GCD in C
Right said Satyan, even I was about to post the same.
Thanks Shabbir for the modified code for the loop. 
Re: Finding LCM & GCD in C
Quote:

Re: Finding LCM & GCD in C
Quote:
The correct code would be: Code:
for(n=1;n%a != 0 && n%b != 0;n++); 
Re: Finding LCM & GCD in C
thanks buddy.....this might short'n my calcutaion

Re: Finding LCM & GCD in C
BlasterBlang, this is the third time I have been telling you to confine links to signatures only.

Re: Finding LCM & GCD in C
ok...fine

Re: Finding LCM & GCD in C
but link are not enable in signatures

Re: Finding LCM & GCD in C
They will appear once on a page. You will see it on the seventh post of the thread.

Re: Finding LCM & GCD in C
hey guys,
Was just interested if with the GCD function if its possible to find the GCD of 3 different numbers rather than just 2? Would be interested to see it. Thanks 
Re: Finding LCM & GCD in C
helloo

Re: Finding LCM & GCD in C
liked the way to do manipulations done in LCM and GCD programs

Re: Finding LCM & GCD in C
hi everyone!
I have code following: You see and shift it Code:
struct ths 
Re: Finding LCM & GCD in C
Offtopic comment:
Use code tag ([ code][/code ]) when you have code snippets in the post. 
Re: Finding LCM & GCD in C
Quote:
this is code in C++, how do you see ? ofcourse, Class in C++ is difficult. What way do you help me to express easy? 
Re: Finding LCM & GCD in C
hi everybody!
Follow you, Can we do in swap 2 structs easier? Code:
tmp = ts[i] ; 
Re: Finding LCM & GCD in C
You can swap the struct if they are addresses and not the objects can be done in that way unless they have the overloaded = operator.

Re: Finding LCM & GCD in C
thank, but can we do easier for it ?
not tmp. 
Re: Finding LCM & GCD in C
Lets see the performance.
:confused: Which is the fastest algorithm? :) GCM seems to be at most n :( LCM seems to be at most n*m :p so, if you do lcm(a,b) := (a*b)/gcm(a,b), you get a n complexity :eek: This is mathematics. 
Re: Finding LCM & GCD in C
Swamp is the subject of another post:
http://www.go4expert.com/showthread.php?t=1732 
Re: Finding LCM & GCD in C
Quote:

Re: Finding LCM & GCD in C
clocking and shabbir are speaking of swaping variables.
This subject was already spoken in the other thread. Just that 
Re: Finding LCM & GCD in C
:) hi !
thanks for your idea. But I only want someone tell me about that algorithm. Swapping is not difficult, but I'm interested in the easier way solving it. Code:
tmp = ts[i]; 
Re: Finding LCM & GCD in C
Hi,
This is Bala. I want to know how to calculate GCD of many numbers, ex 2048 values. Please help. Thanks in advance. 
Re: Finding LCM & GCD in C
Code:
int main() 
Re: Finding LCM & GCD in C
can you please tell me when the loop will actually stop

Re: Finding LCM & GCD in C
correct code is this 100% checked!
for(n=1;n%a != 0  n%b != 0;n++); return n 
Re: Finding LCM & GCD in C
When I searched for lcm c++, this thread had the top two spots, so I figured that I'd join and put this here.
For finding the LCM (Least Common Multiple) of two integers, a and b, in C++, use: for(n=a;n%b != 0;n+=a); return n; For maximum speed, try to set it up so that the larger number is "a" and the smaller one is "b". The reasoning: The least common multiple (LCM) of two numbers, a and b, is the smallest number that is a multiple of "a" AND a multiple of "b". If we already know that the LCM is a multiple of "a", then why not count by "a"? When we count by "a", starting at "a", we are testing every number that is a multiple of "a" already, and only have to see if it is a multiple of "b" too. This code does just that. When "a", the number we're counting by, is larger than 1, the process will be much faster than with those other suggestions, because it's taking bigger steps (at the same speed per step) to get to the same solution. If "a" is a billion, then this will be at least a billion times faster than this: for(n=1;n%a != 0  n%b != 0;n++); return n; (even though both work) I hope this helps. 
Re: Finding LCM & GCD in C
For lcm try this one:
int lcm(int a,int b) { int n; if(a<b) { n=a; a=b; b=n; } for(n=a;n%b!=0;n+=a) return n; }:happy: 
Re: Finding LCM & GCD in C
After 8 posts I would have thought you would know about code blocks by now. USE THEM PLEASE.

Re: Finding LCM & GCD in C
For lcm try this one:
Code:
int lcm(int a;int b) 
Re: Finding LCM & GCD in C
i had written that function using blocks.i don't know what happened when i posted it.

Re: Finding LCM & GCD in C
Added that for you now.

Re: Finding LCM & GCD in C
thank you.

Re: Finding LCM & GCD in C
Actually, I used the code on the first page to correct my code 'til I got it right. Needed this for a take home exam for C class.
Thanks, and in return I'll give you my code which might help someone. Code:
int gcd( int x, int y) { 
Re: Finding LCM & GCD in C
Wouldn't this code also cover both cases of GCD whether your first variable is bigger or your second variable is bigger?
Code:
void gcd(int x, int y) 
Re: Finding LCM & GCD in C
Quote:

Re: Finding LCM & GCD in C
Quote:

All times are GMT +5.5. The time now is 10:20. 