text‹
The attentive reader may relate this ad-hoc implementation to the
arithmetic notion of prime numbers as a little exercise. ›
primrec mark :: "nat \ nat \ bool list \ bool list"where "mark _ _ [] = []"
| "mark m n (p # ps) = (case n of 0 \ False # mark m m ps
| Suc n ==> p # mark m n ps)"
lemma length_mark [simp]: "length (mark m n ps) = length ps" by (induct ps arbitrary: n) (simp_all split: nat.split)
function sieve :: "nat \ bool list \ bool list"where "sieve m ps = (case dropWhile Not ps
of [] ==> ps
| p#ps' \ let n = m - length ps'in takeWhile Not ps @ p # sieve m (mark n n ps'))" by pat_completeness auto
termination🍋‹tuning of this proofis left as an exercise to the reader› apply (relation "measure (length \ snd)") apply rule apply (auto simp add: length_dropWhile_le) proof - fix ps qs q assume"dropWhile Not ps = q # qs" thenhave"length qs < length (dropWhile Not ps)" by simp alsohave"length (dropWhile Not ps) \ length ps" by (simp add: length_dropWhile_le) finallyshow"length qs < length ps" . qed
primrec natify :: "nat \ bool list \ nat list"where "natify _ [] = []"
| "natify n (p#ps) = (if p then n # natify (Suc n) ps else natify (Suc n) ps)"
primrec list_primes where "list_primes (Suc n) = natify 1 (sieve n (False # replicate n True))"
subsubsection ‹Naive factorisation›
function factorise_from :: "nat \ nat \ nat list"where "factorise_from k n = (if 1 < k \ k \ n then let (q, r) = Euclidean_Rings.divmod_nat n k inif r = 0 then k # factorise_from k q
else factorise_from (Suc k) n
else [])" by pat_completeness auto
termination factorise_from 🍋‹tuning of this proofis left as an exercise to the reader› apply (relation "measure (\(k, n). 2 * n - k)") apply (auto simp add: Euclidean_Rings.divmod_nat_def algebra_simps elim!: dvdE)
subgoal for m n apply (cases "m \ n * 2") apply (auto intro: diff_less_mono) done done
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.