Skip to content

Instantly share code, notes, and snippets.

@rindPHI
Created May 3, 2017 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rindPHI/8d7a2415f16599e0b77b01d330f5c811 to your computer and use it in GitHub Desktop.
Save rindPHI/8d7a2415f16599e0b77b01d330f5c811 to your computer and use it in GitHub Desktop.
Method computing the Greatest Common Divisor (GCD) of two integers
/*@ public normal_behavior
@ ensures (a != 0 || b != 0) ==>
@ (a % \result == 0 && b % \result == 0 &&
@ (\forall int x; x > 0 && a % x == 0 && b % x == 0;
@ \result % x == 0));
@*/
public static int gcd(int a, int b) {
if (a < 0) a = -a;
if (b < 0) b = -b;
int big, small;
if (a > b) {
big = a;
small = b;
}
else {
big = b;
small = a;
}
return gcdHelp(big, small);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment