Summation of primes: Difference between revisions
Content deleted Content added
Added AppleScript solutions. |
|||
Line 38: | Line 38: | ||
142 913 828 922 |
142 913 828 922 |
||
</pre> |
</pre> |
||
=={{header|AppleScript}}== |
|||
This isn't something that's likely to needed more than once — if at all — so you'd probably just throw together code like the following. The result's interesting in that although it's way outside AppleScript's integer range, its class is returned as integer in macOS 10.14 (Mojave)! |
|||
<lang applescript>on isPrime(n) |
|||
if ((n < 4) or (n is 5)) then return (n > 1) |
|||
if ((n mod 2 = 0) or (n mod 3 = 0) or (n mod 5 = 0)) then return false |
|||
repeat with i from 7 to (n ^ 0.5) div 1 by 30 |
|||
if ((n mod i = 0) or (n mod (i + 4) = 0) or (n mod (i + 6) = 0) or ¬ |
|||
(n mod (i + 10) = 0) or (n mod (i + 12) = 0) or (n mod (i + 16) = 0) or ¬ |
|||
(n mod (i + 22) = 0) or (n mod (i + 24) = 0)) then return false |
|||
end repeat |
|||
return true |
|||
end isPrime |
|||
on sumPrimes below this |
|||
set limit to this - 1 |
|||
if (limit < 2) then return 0 |
|||
set sum to 2 |
|||
repeat with n from 3 to limit by 2 |
|||
if (isPrime(n)) then set sum to sum + n |
|||
end repeat |
|||
return sum |
|||
end sumPrimes |
|||
sumPrimes below 2000000</lang> |
|||
{{output}} |
|||
<lang applescript>142913828922</lang> |
|||
The result can be obtained in 4 seconds rather than 14 if the summing's instead combined with an Eratosthenean sieve: |
|||
<lang applescript>on sumPrimes below this |
|||
set limit to this - 1 |
|||
-- Is the limit 2 or lower? |
|||
if (limit = 2) then return 2 |
|||
if (limit < 2) then return 0 |
|||
-- Build a list of limit (+ 2 for safety) missing values. |
|||
set mv to missing value |
|||
script o |
|||
property numberList : {mv} |
|||
end script |
|||
repeat until ((count o's numberList) * 2 > limit) |
|||
set o's numberList to o's numberList & o's numberList |
|||
end repeat |
|||
set o's numberList to {mv} & items 1 thru (limit - (count o's numberList) + 1) of o's numberList & o's numberList |
|||
-- Populate every 6th slot after the 5th and 7th with the equivalent integers. |
|||
-- The other slots all represent multiples of 2 and/or 3 and are left as missing values. |
|||
repeat with n from 5 to limit by 6 |
|||
set item n of o's numberList to n |
|||
tell (n + 2) to set item it of o's numberList to it |
|||
end repeat |
|||
-- If we're here, the limit must be 3 or higher. So start with the sum of 2 and 3. |
|||
set sum to 5 |
|||
-- Continue adding primes from the list and eliminate multiples |
|||
-- of those up to the limit's square root from the list. |
|||
set isqrt to limit ^ 0.5 div 1 |
|||
repeat with n from 5 to limit by 6 |
|||
if (item n of o's numberList = n) then |
|||
set sum to sum + n |
|||
if (n ≤ isqrt) then |
|||
repeat with multiple from (n * n) to limit by n |
|||
set item multiple of o's numberList to mv |
|||
end repeat |
|||
end if |
|||
end if |
|||
tell (n + 2) |
|||
if ((it ≤ limit) and (item it of o's numberList = it)) then |
|||
set sum to sum + it |
|||
if (it ≤ isqrt) then |
|||
repeat with multiple from (it * it) to limit by it |
|||
set item multiple of o's numberList to mv |
|||
end repeat |
|||
end if |
|||
end if |
|||
end tell |
|||
end repeat |
|||
return sum |
|||
end sumPrimes |
|||
sumPrimes below 2000000</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang AWK> |
<lang AWK> |