- #1
MathematicalPhysicist
Gold Member
- 4,699
- 371
i have to write the function gcd for c programme.
here's my code:
so is this correct? I am using the fact that the divison of two integers returns an integer solution, i.e without the remainder, and ofcourse euclid's algorithm.
p.s
this is a part of a programme that i need to do in order to check for pythaogrean triplets, so it's not supposed to run alone.
thanks in advance.
here's my code:
Code:
int gcd(int m, int n)
{
int s,r,x,y;
if (m>=n)
{
r=m-(m/n)*n;
for(x=m,y=n;r!=0;x=y,y=r)
r=x-(x/y)*y;
}
if(n>m)
{
r=n-(n/m)*n;
for(x=n,y=m;r!=0;x=y,y=r)
r=x-(x/y)*y;
}
return(r);
}
p.s
this is a part of a programme that i need to do in order to check for pythaogrean triplets, so it's not supposed to run alone.
thanks in advance.