Commit 84d4ed32 authored by Martin Clochard's avatar Martin Clochard
Browse files

euler003 example completed.

parent d49c6573
......@@ -12,21 +12,29 @@ module PrimeFactor
use import int.ComputerDivision
use import number.Divisibility
use import number.Prime
use import number.Coprime
(** returns the smallest divisor of [n] greater or equal to [d] *)
(** returns the smallest divisor of [n] greater then 1,
assuming no divisor between 2 and [d]. *)
let rec smallest_divisor (d n:int) : int
requires { 2 <= n }
requires { 1 <= d <= n }
requires { 2 <= n }
requires { 2 <= d <= n }
requires { forall i:int. 2 <= i < d -> not divides i n }
ensures { d <= result <= n }
ensures { divides result n }
ensures { forall i:int. d <= i < result -> not divides result i }
ensures { forall i:int. 2 <= i < result -> not divides i n }
variant { n - d }
= if d * d > n then n else
if d >= 2 && mod n d = 0 then d else
= if d * d > n then begin
assert { forall i:int. 2 <= i < n /\ divides i n ->
i >= d && let u = div n i in u * i = n && divides u n &&
u * i = n && (u >= d -> n >= d * i >= d * d && false)
&& u >= 2 && u < n && false } ; n
end else if d >= 2 && mod n d = 0 then d else
smallest_divisor (d+1) n
(* The tests are invalidated by the new precondition. *)
let test2 () = smallest_divisor 2 13195 (* 5 *)
(*let test2 () = smallest_divisor 2 13195 (* 5 *)
let test5 () = smallest_divisor 5 13195 (* 5 *)
......@@ -36,7 +44,7 @@ let test8 () = smallest_divisor 8 13195 (* 13 *)
let test14 () = smallest_divisor 14 13195 (* 29 *)
let test30 () = smallest_divisor 30 13195 (* 35 *)
let test30 () = smallest_divisor 30 13195 (* 35 *)*)
use import ref.Ref
......@@ -49,21 +57,40 @@ let largest_prime_factor (n:int) : int
ensures { prime result }
ensures { divides result n }
ensures { forall i:int. result < i <= n -> not (prime i /\ divides i n) }
= let d = smallest_divisor 1 n in
= let d = smallest_divisor 2 n in
let factor = ref d in
let target = ref (div n d) in
factors := Cons d Nil;
assert { !target * d = n && divides !target n } ;
assert { forall i:int. prime i /\ divides i n /\ i > d ->
coprime d i && divides i !target };
while !target >= 2 do
invariant { !target = 1 \/ 2 <= !factor <= !target }
(* invariant { 2 <= !factor <= n } *)
(* invariant { divides !target n } *)
invariant { 1 <= !target <= n }
invariant { 2 <= !factor <= n }
invariant { divides !factor n }
invariant { prime !factor }
variant { !target }
invariant { forall i:int. divides i !target /\ i >= 2 ->
i >= !factor /\ divides i n }
invariant { forall i:int. prime i /\ divides i n /\ i > !factor ->
divides i !target }
(*invariant { !target = 1 \/ 2 <= !factor <= !target }*)
(* invariant { 2 <= !factor <= n } *)
(* invariant { divides !target n } *)
(*invariant { divides !factor n }*)
(*invariant { prime !factor }*)
variant { !target }
let ghost oldt = !target in
let ghost oldf = !factor in
assert { divides !target !target && !target >= 2 && !target >= !factor };
let d = smallest_divisor !factor !target in
assert { prime d };
factor := d;
factors := Cons d !factors;
target := div !target d
target := div !target d;
assert { !target * d = oldt && divides !target oldt } ;
assert { forall i:int. prime i /\ divides i n /\ i > d ->
i > oldf && divides i oldt && 1 <= d < i
&& coprime d i && divides i !target }
done;
!factor
......
This diff is collapsed.
This diff is collapsed.
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