Skip to content

Instantly share code, notes, and snippets.

@3014zhangshuo
Created June 16, 2020 01:47
Show Gist options
  • Save 3014zhangshuo/58f646cebe85e21caf3fdeef9d974d66 to your computer and use it in GitHub Desktop.
Save 3014zhangshuo/58f646cebe85e21caf3fdeef9d974d66 to your computer and use it in GitHub Desktop.
Euclid's algorithm
public static int gcd(int p, int q)
{
if (q == 0) return p;
int r = p % q;
return gcd(q, r);
}
@3014zhangshuo
Copy link
Author

Compute the greatest common divisor of two nonnegative integers p and q as follows: If q is 0, answer is p. If not, divide p by q and take the remainder r. The answer is the greatest common divisor of q and r.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment