39.9k views
2 votes
Write a function gcd(x, y)that returns the greatest common divisor of the parameters candy. You can use the Euclidean algorithm, which uses the fact that gcd(x,y) = gcd(modx,x). Say you want to nd the gcd of 462 and 1071

User Mr Bean
by
8.0k points

1 Answer

3 votes

Step-by-step explanation:

In some language, the function can be written recursively using the correct Euclidean algorithm as ...

gcd[y_, x_] := If[Mod[y,x]==0, x, gcd[x, Mod[y,x]]]

Using this on the given numbers, you get ...

gcd[462, 1071] = 21

Write a function gcd(x, y)that returns the greatest common divisor of the parameters-example-1