Jump to content

The sieve of Sundaram: Difference between revisions

m
m (→‎{{header|AppleScript}}: Optimisation.)
Line 115:
end repeat
-- Apply the sieve, replacingstoring survivingderived 'true'sprimes in theconsecutive listslots withfrom theirthe indexbeginning *of 2the + 1list.
set step to 1
set item 1 of o's lsti to 31
set item i of o's lst to 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
tell (n + 1) to if (item it of o's lst) then set item it of o's lsti to it * 2i + 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
repeat with j from (n + 2 + step) to limit by step
Line 126 ⟶ 134:
end repeat
end repeat
-- set beginning of o's lst to 2 -- Uncomment if neededrequired.
return o's lst's numbersitems n1 thru n2 of o's lst
end sieveOfSundaram
 
557

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.