while y ≤ a do
invariant x^2 ≤ a ∧ y = x^2+z ∧ z = 2*x+1;
+ decreases a-x^2;
{
x=x+1;
z=z+2;
proc SetMin(s:Set[nat]): nat
requires s ≠ ∅[nat];
+ requires ∀e ∈ s. e ≤ MAX;
ensures result ∈ s ∧ ∀e ∈ s. result ≤ e;
{
var ret:nat := MAX;
for x in s do
+ invariant ∀e in forSet. ret ≤ e;
{
if x < ret then
{
ensures result ∈ s ∧ ∀e ∈ s. result ≤ e;
= choose m in s with ∀e in s. m <= e;
+theorem PrimeDivisor() ⇔ ∀e:nat. (e > 1 ∧ ¬isPrime(e)) ⇒ ∃p:nat. p<e ∧ isPrime(p) ∧ e%p = 0;
+
proc SieveOfErathostenesSet(m:nat): Set[nat]
ensures ∀e:nat. e ≤ m ⇒ (e ∈ result ⇔ isPrime(e));
{
var ret:Set[nat] := ∅[nat];
while start ≠ ∅[nat] do
- invariant ret ⊆ 2..m ∧ ∀e ∈ ret. isPrime(e);
+ decreases |start|;
+ invariant start ⊆ 2..m ∧ ret ⊆ 2..m;
+ invariant ∀e ∈ ret. isPrime(e);
+ invariant start ≠ ∅[nat] ⇒ ∀e ∈ ret. e < SetMin(start);
+ invariant ∀e:nat. e ≤ m ∧ e ∈ start ⇒ ¬ (e ∈ ret);
{
var locmi:nat = SetMin(start);
ret = ret ∪ {locmi};
}
+
+