Sieve of Eratosthenes: Difference between revisions
Content deleted Content added
→{{header|Lambdatalk}}: improving code |
|||
Line 11,283:
{def rsieve
{
{lambda {:n}▼
▲ {lambda {:n :a :i}
{
else}}}▼
{+ :i 1}}
▲ {lambda {:a :n :i :k}
{def rsieve.loop▼
▲ then {rsieve.mark :a :n :i {* :i :i}}
▲ else :a}
▲ :n {+ :i 1}}
{lambda {:n}
{S.replace \s by space in
{rsieve.disp▼
{S.replace (\[|\]|\.|,) by space in
{
{A.slice 2 -1
-> rsieve
{
->
Note:
2) iterative version {isieve n}
Line 11,332 ⟶ 11,320:
{def isieve
{def isieve.
{def isieve.disp▼
▲ {S.serie 0 {- {A.length :a} 1}}}}}
▲ {S.serie {* :i :i} :n :i} }}}
{lambda {:n}
{S.replace \s by space in
{S.replace (\[|\]|\.|,) by space in
{S.map {{lambda {:n :a :i} {if {W.equal?
} :n {A.new {S.serie 0 :n}}}
{S.serie 2 {sqrt :n} 1}}}}}}}}}
{isieve
->
Notes:
Note: this version avoids the stackoverflow. From 1 to 1000000 there are 78500 primes (computed in 95000ms) and the last is 999983.▼
- this version avoids stackoverflow.
▲
</syntaxhighlight>
|