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, replacing surviving 'true's in the list with their index * 2 + 1.
-- Apply the sieve, storing derived primes in consecutive slots from the beginning of the list.
set step to 1
set step to 1
set item 1 of o's lst to 3
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 set item n of o's lst to n * 2 + 1
if (item n of o's lst) then
tell (n + 1) to if (item it of o's lst) then set item it of o's lst to it * 2 + 1
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 needed.
-- set beginning of o's lst to 2 -- Uncomment if required.
return o's lst's numbers n1 thru n2
return items n1 thru n2 of o's lst
end sieveOfSundaram
end sieveOfSundaram