Guest User

Untitled

a guest
May 26th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. //@+ CheckArithOverflow = no
  2.  
  3. /*@ lemma mod2_div2:
  4. @ \forall integer x y; x % 2 == y % 2 ==> (x + y)/2 - x == y - (x + y)/2;
  5. @*/
  6.  
  7. public class Mean {
  8.  
  9. /*@ requires x % 2 == y % 2;
  10. @ ensures \result == (x + y)/2;
  11. @*/
  12. public static int mean(int x, int y) {
  13. int i = x;
  14. int j = y;
  15.  
  16. /*@ loop_invariant (i < j ==> (x + y)/2 - i == j - (x + y)/2) &&
  17. (i >= j ==> (x + y)/2 - j == i - (x + y)/2); */
  18. while (i != j) {
  19. if (i < j) {
  20. i = i + 1;
  21. j = j - 1;
  22. } else {
  23. j = j + 1;
  24. i = i - 1;
  25. }
  26. }
  27.  
  28. return i;
  29. }
  30.  
  31. }
Add Comment
Please, Sign In to add comment