Commit 792bc1a4 by Jean-Christophe Filliâtre

Sieve of Eratosthenes slightly improved

parent 7b0513b1
 (** Sieve of Eratosthenes Author: Martin Clochard See also knuth_prime_numbers.mlw in the gallery. *) module Sieve use import int.Int use import array.Array use import ref.Ref use import number.Prime predicate no_factor_lt (bnd num:int) = num > 1 /\ forall k l. 1 < l < bnd /\ k > 1 -> num <> k * l ... ... @@ -25,15 +33,17 @@ module Sieve invariant { forall j. 0 <= j < n -> t[j] <-> no_factor_lt !i j } variant { n - !i } if t[!i] then begin let r = ref !i in while !r * !i < n do invariant { 1 < !r <= n } let s = ref (!i * !i) in let ghost r = ref !i in while !s < n do invariant { 1 < !r <= n /\ !s = !r * !i } invariant { forall j. 0 <= j < n -> t[j] <-> (no_factor_lt !i j /\ forall k. 1 < k < !r -> j <> k * !i) } variant { n - !r } t[!r * !i] <- false; incr r; t[!s] <- false; s := !s + !i; incr r done; assert { forall j. 0 <= j < n /\ t[j] -> (forall k l. 1 < l < !i + 1 -> j = k * l /\ k > 1 -> ... ... @@ -50,6 +60,6 @@ module Sieve assert { forall j. 0 <= j < n /\ prime j -> forall k l. 1 < l < n /\ k > 1 -> j = k * l -> l >= j && false }; t end
 ... ... @@ -4,14 +4,16 @@ ... ... @@ -40,47 +42,57 @@ ... ...
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!