I'm trying to reduce a fraction and I need to find the GCF. 20/8 would be reduced to 5/2. How would I find the 4?

Printable View

- 06-09-2002NavyBlueGreatest Common Factor
I'm trying to reduce a fraction and I need to find the GCF. 20/8 would be reduced to 5/2. How would I find the 4?

- 06-09-2002Sebastiani
You should thank me: I coded this by hand for you in 3 minutes!

Code:

int greatest_common_factor( int a, int b )

{

int i;

int result = 0;

for(i = 1; i <= a && i <= b; i++)

{

if( a % i == 0 && b % i == 0)

result = i;

}

return result;

}

- 06-09-2002fyodor
there is actually a much more elegant algorithm-look up Euclid's Algorithm

- 06-10-2002UnregisteredGreatest Common Factor
Hi idiots,

there is a recrsive function you may wish to use....

{in pascal code... haha, C-style syntax sucks ass!}

function GCF (a,b: integer): integer;

begin

if b<> 0

then

GCF:= GCF(b, a mod b)

else

GCF:= a;

end;

Computer science is the study of problem-solving, not of learning how to code in fifteen-thousand languages, you damn turds! - 06-11-2002Shiro
>Computer science is the study of problem-solving, not of

>learning how to code in fifteen-thousand languages, you damn

>turds!

This is the C-board, one of which goals is to learn C. - 06-11-2002hobbes
Here is how it is in C:

int GCF(int a,int b)

{

int gcf;

if(b!=0)

gcf=GCF(b, a%b);

else gcf=a;

return gcf;

}