Skip to content

Instantly share code, notes, and snippets.

@rindPHI
Created May 3, 2017 13:09
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/5765c26f57c35d62f6dbeb11dca57012 to your computer and use it in GitHub Desktop.
Save rindPHI/5765c26f57c35d62f6dbeb11dca57012 to your computer and use it in GitHub Desktop.
Gcd method with merge point specifications
/*@ 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;
//@ merge_point
//@ merge_proc "MergeByIfThenElse"; // optional
if (b < 0) b = -b;
//@ merge_point
//@ merge_proc "MergeByPredicateAbstraction"
//@ merge_params {
//@ conjunctive:
//@ (int x) -> {x >= 0, (x == \old(b) || x == -\old(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