isqrt.c 684 Bytes
Newer Older
MARCHE Claude's avatar
MARCHE Claude committed
1 2 3 4
/* run.config
   OPT: -journal-disable -jessie3
*/

MARCHE Claude's avatar
MARCHE Claude committed
5 6 7 8

//@ logic integer sqr(integer x) = x * x;

/*@ requires x >= 0;
MARCHE Claude's avatar
MARCHE Claude committed
9
  @ ensures \result >= 0 ; // && sqr(\result) <= x && x < sqr(\result + 1);
MARCHE Claude's avatar
MARCHE Claude committed
10 11 12
  @*/
int isqrt(int x) {
  int count = 0, sum = 1;
MARCHE Claude's avatar
MARCHE Claude committed
13
  /*@ loop invariant count >= 0 && x >= sqr(count) ; // && sum == sqr(count+1);
MARCHE Claude's avatar
MARCHE Claude committed
14 15
    @ loop variant  x - count; 
    @*/
16
  while (sum <= x) { ++count; sum += 2 * count + 1; }
MARCHE Claude's avatar
MARCHE Claude committed
17 18 19
  return count;
}

20
#if 0
MARCHE Claude's avatar
MARCHE Claude committed
21 22 23 24 25 26 27 28 29
//@ ensures \result == 4;
int main () {
  int r;
  r = isqrt(17);
  //@ assert r < 4 ==> \false;
  //@ assert r > 4 ==> \false;
  return r;
}

30 31
#endif

MARCHE Claude's avatar
MARCHE Claude committed
32 33 34 35 36 37 38
/*
Local Variables:
compile-command: "frama-c -add-path ../.. -jessie3 isqrt.c"
End:
*/