The sieve of Sundaram: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: changed the whitespace in the REXX section header. |
m →{{header|AppleScript}}: Further optimised by only marking two out of every three "numbers" in each sweep. Minor cosmetic changes. |
||
Line 86:
=={{header|AppleScript}}==
<lang applescript>on sieveOfSundaram(
if (
set n1 to beginning of
set n2 to end of
else
set n1 to
set n2 to
end if
Line 99:
end script
-- Build a list of at least as many 'true's as there are notionally initially
--
set {unmarked, marked} to {true, false}
-- The Python and Julia solutions note that the nth prime is approx n * 1.2 * log(n),
-- but the number from which it'
--
set limit to (do shell script "echo '" & n2 & " * 0.6 * l(" & n2 & ") +
set len to 1500
repeat len times
Line 115:
end repeat
-- Apply the sieve, storing
set step to 1
set i to 1
Line 122:
if (item n of o's lst) then
set i to i + 1
set item i of o's lst to n + n + 1 -- (n * 2 + 1)
end if
set i to i + 1
set item i of o's lst to
end if
if (i ≥ n2) then exit repeat -- Enough primes obtained.
set step to step + 2
-- depending on where the count starts) can be omitted, since
-- it'll be covered by other sweeps or the slot overwritten.
repeat with j from (n + 2 + step) to (limit - step) by step * 3
set item j of o's lst to marked
set item (j + step) of o's lst to marked
end repeat
end repeat
|