Commit a7b86a0c by Jean-Christophe Filliâtre

new example: integer square root by Newton's methode (credits go to Claude)

parent e3e2852c
 (* Integer square root *) module M module Simple use import int.Int use import module ref.Ref ... ... @@ -28,6 +28,35 @@ module M end (* code and proof by Claude Marché, 2002 *) module NewtonMethod use import int.Int use import int.ComputerDivision use import module ref.Ref let sqrt = fun (x : int) -> { x >= 0 } if x = 0 then 0 else if x <= 3 then 1 else let y = ref x in let z = ref (div (x+1) 2) in while !z < !y do invariant { !z > 0 /\ !y > 0 /\ !z = div (div x !y + !y) 2 /\ x < (!y + 1) * (!y + 1) /\ x < (!z + 1) * (!z + 1) } variant { !y } y := !z; z := div (div x !z + !z) 2 done; !y { result * result <= x < (result+1) * (result+1) } end (* Local Variables: compile-command: "unset LANG; make -C ../.. examples/programs/isqrt.gui" ... ...
 ... ... @@ -2,7 +2,7 @@ ... ... @@ -17,5 +17,41 @@
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment