TP-modelisation/TP6/division.mlw

24 lines
499 B
Plaintext
Raw Normal View History

2023-06-10 18:56:24 +00:00
(* Division entière par l'algorithme d'Euclide *)
module Division
use int.Int
use ref.Refint
let division (a b: int) (* renvoie une paire d'entiers (quotient,reste) *)
requires { 0 <= a && 0 < b }
ensures { let (q,r) = result in q * b + r = a && 0 <= r < b }
=
let q = ref 0 in
let r = ref a in
while !r >= b do
invariant { a = !q * b + !r && 0 <= !q && 0 <= !r }
variant { !r - b }
q := (!q) + 1;
r := (!r) - b
done;
(!q,!r)
end