The sieve of Sundaram: Difference between revisions

m
→‎{{header|AppleScript}}: Further optimised by only marking two out of every three "numbers" in each sweep. Minor cosmetic changes.
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(rangeindexRange)
if (rangeindexRange's class is list) then
set n1 to beginning of rangeindexRange
set n2 to end of rangeindexRange
else
set n1 to rangeindexRange
set n2 to rangeindexRange
end if
Line 99:
end script
-- Build a list of at least as many 'true's as there are notionally initially unmarked numbers from which
-- theunmarked primes will be calculatednumbers. The numbers themselves are implied by theirthe indices.
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'sll be derived will beis about half that.
-- 215 is added too here to ensure headroom forwhen working with lower prime counts.
set limit to (do shell script "echo '" & n2 & " * 0.6 * l(" & n2 & ") + 215'| bc -l") as integer
set len to 1500
repeat len times
Line 115:
end repeat
-- Apply the sieve, storing derivedgenerated primes in consecutive slots from the beginning of the list.
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
tellif (item (n + 1) to if (item it of o's lst) then
set i to i + 1
set item i of o's lst to itn + n + 3 -- ((n + 1) * 2 + 1)
end if
if (i ≥ n2) then exit repeat -- Enough primes obtained.
set step to step + 2
repeat-- withThe jfirst fromof (n/every +three/ 2markings +in step)each tosweep limit(or bythe stepthird,
-- 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
557

edits