From 072cd08324c3348ae465e8c5f35cb5d9ca64e05a Mon Sep 17 00:00:00 2001 From: Christoph Fuerst Date: Tue, 11 Apr 2017 21:59:06 +0200 Subject: [PATCH] Added sieve of erathostenes --- sieveEratosthenes.txt | 73 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/sieveEratosthenes.txt b/sieveEratosthenes.txt index 77b5d46..4790088 100644 --- a/sieveEratosthenes.txt +++ b/sieveEratosthenes.txt @@ -1,33 +1,53 @@ - val MAX: ℕ; type nat = ℕ[MAX]; pred divides(m:nat,n:nat) ⇔ ∃p:nat. m⋅p = n; -pred isPrime(m:nat) ⇔ 1 < m ∧ ∀n:nat. divides(n,m) ⇒ (n≤1 ∨ n=m); +pred isPrime(m:nat) ⇔ (m = 0) ∨ (m=1) ∨ + (1 < m ∧ ∀n:nat. divides(n,m) ⇒ (n≤1 ∨ n=m)); -proc SieveOfErathostenes(m:ℕ[MAX]): Array[MAX,nat] - requires m>1; - ensures ∀p:ℕ[MAX]. result[p]=1 ⇒ isPrime(p+1); + +proc IntegerRoot(a:nat): nat + ensures result^2 ≤ a ∧ a < (result+1)^2; { - var ret:Array[MAX,nat] := Array[MAX,nat](0); - var i:ℕ[MAX]; - var j:ℕ[2*MAX]; - - ret[0] = 1; - ret[1] = 1; - -// for i=2;i1; + ensures ∀p:nat. ((p < m ∧ result[p]=true) ⇒ isPrime(p)); +{ + var ret:Array[MAX,Bool] := Array[MAX,Bool](true); + var i:nat; + var k:nat = 0; + var j:nat; + + for i=2; i≤IntegerRoot(m); i=i+1 do + decreases IntegerRoot(m)+1-i; + { + if ret[i] = true then + { + j = i^2; + for k=0;i^2+k*i