Skip to content

Instantly share code, notes, and snippets.

@ttezel
Last active December 11, 2015 17:38
Show Gist options
  • Save ttezel/4635578 to your computer and use it in GitHub Desktop.
Save ttezel/4635578 to your computer and use it in GitHub Desktop.
gcd algorithm (extended-euclid)
%{
Problem 7 (ii) - extended-euclid function
Given two integers a and b, it finds the largest integer that
divides both of them - known as their greatest common divisor (gcd).
Input: integers a and b where a >= b >= 0
Output : gcd(a, b)
%}
function result = euclid (a, b)
if (b == 0)
result = a
return;
end
result = euclid(b, mod(a, b));
return;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment