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: | Line 86: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
<lang applescript>on sieveOfSundaram( |
<lang applescript>on sieveOfSundaram(indexRange) |
||
if ( |
if (indexRange's class is list) then |
||
set n1 to beginning of |
set n1 to beginning of indexRange |
||
set n2 to end of |
set n2 to end of indexRange |
||
else |
else |
||
set n1 to |
set n1 to indexRange |
||
set n2 to |
set n2 to indexRange |
||
end if |
end if |
||
Line 99: | Line 99: | ||
end script |
end script |
||
-- Build a list of at least as many 'true's as there are initially |
-- Build a list of at least as many 'true's as there are notionally initially |
||
-- |
-- unmarked numbers. The numbers themselves are implied by the indices. |
||
set {unmarked, marked} to {true, false} |
set {unmarked, marked} to {true, false} |
||
-- The Python and Julia solutions note that the nth prime is approx n * 1.2 * log(n), |
-- The Python and Julia solutions note that the nth prime is approx n * 1.2 * log(n), |
||
-- but the number from which it' |
-- but the number from which it'll be derived is about half that. |
||
-- |
-- 15 is added too here to ensure headroom when working with lower prime counts. |
||
set limit to (do shell script "echo '" & n2 & " * 0.6 * l(" & n2 & ") + |
set limit to (do shell script "echo '" & n2 & " * 0.6 * l(" & n2 & ") + 15'| bc -l") as integer |
||
set len to 1500 |
set len to 1500 |
||
repeat len times |
repeat len times |
||
Line 115: | Line 115: | ||
end repeat |
end repeat |
||
-- Apply the sieve, storing |
-- Apply the sieve, storing generated primes in consecutive slots from the beginning of the list. |
||
set step to 1 |
set step to 1 |
||
set i to 1 |
set i to 1 |
||
Line 122: | Line 122: | ||
if (item n of o's lst) then |
if (item n of o's lst) then |
||
set i to i + 1 |
set i to i + 1 |
||
set item i of o's lst to n * 2 + 1 |
set item i of o's lst to n + n + 1 -- (n * 2 + 1) |
||
end if |
end if |
||
if (item (n + 1) of o's lst) then |
|||
set i to i + 1 |
set i to i + 1 |
||
set item i of o's lst to |
set item i of o's lst to n + n + 3 -- ((n + 1) * 2 + 1) |
||
end if |
end if |
||
if (i ≥ n2) then exit repeat -- Enough primes obtained. |
if (i ≥ n2) then exit repeat -- Enough primes obtained. |
||
set step to step + 2 |
set step to step + 2 |
||
-- The first of /every three/ markings in each sweep (or the third, |
|||
-- 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 of o's lst to marked |
||
set item (j + step) of o's lst to marked |
|||
end repeat |
end repeat |
||
end repeat |
end repeat |