# I need help with the following code to find greatest common divisor.

Alain Fallada

Greenhorn

Posts: 18

posted 3 years ago

- 0

What formula are you trying to use to get the GCD? The code isn't right, but I don't know if you've got an incorrect implementation of the right algorithm, or a correct implementation of the wrong algorithm. At the moment you're just taking the modulus of one of the values with respect to the other, which isn't correct.

Take the example you've given: 84 and 48. Your code just calculates 84 % 48, which is 36, which isn't a common divisor of either of them. The easiest approach I know is probably the Euclidean algorithm, which uses modular arithmetic, but needs multiple calculations (so either a loop or a recursive function). The recursive calculation goes something like:

Take the example you've given: 84 and 48. Your code just calculates 84 % 48, which is 36, which isn't a common divisor of either of them. The easiest approach I know is probably the Euclidean algorithm, which uses modular arithmetic, but needs multiple calculations (so either a loop or a recursive function). The recursive calculation goes something like:

I agree. Here's the link: http://aspose.com/file-tools |