Sieve of Eratosthenes: Difference between revisions
Content deleted Content added
Not a robot (talk | contribs) Add Modula-2 |
→Bounded sieve: +cmts, var names |
||
Line 13,796: | Line 13,796: | ||
</pre> |
</pre> |
||
Another version, based on Cloksin&Mellish p.175, modified to stop early as well as to work with odds only and use addition in the removing predicate, instead of the <code>mod</code> testing as the original was doing: |
|||
<lang Prolog> |
<lang Prolog>primes(N,[]):- N < 2, !. |
||
primes(N,[2|R]):- ints(3,N,L), sift(N,L,R). |
|||
ints(A,B,[A|C]):- A=<B -> D is A+2, ints(D,B,C). |
|||
ints(_,_,[]). |
|||
sift(_,[],[]). |
|||
sift(N,[A|B],[A|C]):- A*A =< N -> rmv(A,B,D), sift(N,D,C) |
|||
; C=B. |
; C=B. |
||
rmv(A,B,D):- M is A*A, rmv(A,M,B,D). |
|||
rmv(_,_,[],[]). |
|||
rmv(P,M,[A|B],C):- ( M>A -> C=[A|D], rmv(P,M,B,D) |
|||
; M==A -> M2 is M+2*P, |
; M==A -> M2 is M+2*P, rmv(P,M2,B,C) |
||
; M<A -> M2 is M+2*P, |
; M<A -> M2 is M+2*P, rmv(P,M2,[A|B],C) |
||
).</lang> |
).</lang> |
||
===Using lazy lists=== |
===Using lazy lists=== |