The sieve of Sundaram: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: Optimisation.) |
|||
Line 115: | Line 115: | ||
end repeat |
end repeat |
||
-- Apply the sieve, |
-- Apply the sieve, storing derived primes in consecutive slots from the beginning of the list. |
||
set step to 1 |
set step to 1 |
||
set |
set i to 1 |
||
set item i of o's lst to 3 |
|||
repeat with n from 2 to limit by 3 |
repeat with n from 2 to limit by 3 |
||
if (item n of o's lst) then |
if (item n of o's lst) then |
||
set i to i + 1 |
|||
set item i of o's lst to n * 2 + 1 |
|||
end if |
|||
tell (n + 1) to if (item it of o's lst) then |
|||
set i to i + 1 |
|||
set item i of o's lst to it * 2 + 1 |
|||
end if |
|||
if (i ≥ n2) then exit repeat -- Enough primes obtained. |
|||
set step to step + 2 |
set step to step + 2 |
||
repeat with j from (n + 2 + step) to limit by step |
repeat with j from (n + 2 + step) to limit by step |
||
Line 126: | Line 134: | ||
end repeat |
end repeat |
||
end repeat |
end repeat |
||
-- set beginning of o's lst to 2 -- Uncomment if |
-- set beginning of o's lst to 2 -- Uncomment if required. |
||
return |
return items n1 thru n2 of o's lst |
||
end sieveOfSundaram |
end sieveOfSundaram |
||