Primes whose sum of digits is 25: Difference between revisions

Content added Content deleted
(→‎{{header|AppleScript}}: Added an idiomatic solution.)
Line 167: Line 167:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
===Functional===
Not fast. This approach takes over 20 seconds here.
Not fast. This approach takes over 20 seconds here.
<lang applescript>use AppleScript version "2.4"
<lang applescript>use AppleScript version "2.4"
Line 332: Line 333:
{{Out}}
{{Out}}
<pre>[997,1699,1789,1879,1987,2689,2797,2887,3499,3697,3769,3877,3967,4597,4759,4957,4993]</pre>
<pre>[997,1699,1789,1879,1987,2689,2797,2887,3499,3697,3769,3877,3967,4597,4759,4957,4993]</pre>
----
===Idiomatic===

Primes with silly properties are getting a bit tedious. But hey. This takes just under 0.02 seconds.

<lang applescript>on sieveOfEratosthenes(limit)
script o
property numberList : {missing value}
end script
repeat with n from 2 to limit
set end of o's numberList to n
end repeat
repeat with position from 2 to (limit ^ 0.5) div 1
if (item position of o's numberList is not missing value) then
repeat with multiple from position * position to limit by position
set item multiple of o's numberList to missing value
end repeat
end if
end repeat
return o's numberList's numbers
end sieveOfEratosthenes

on numbersWhoseDigitsSumTo(numList, targetSum)
script o
property numberList : numList
property output : {}
end script
repeat with thisNumber in o's numberList
set n to thisNumber's contents
set digitSum to n mod 10
repeat until n is 0
set n to n div 10
set digitSum to digitSum + n mod 10
end repeat
if (digitSum = targetSum) then set end of o's output to thisNumber's contents
end repeat
return o's output
end numbersWhoseDigitsSumTo

-- Task code:
set primesBelow5000 to sieveOfEratosthenes(4999)
set thoseWhoseDigitsSumTo25 to numbersWhoseDigitsSumTo(primesBelow5000, 25)</lang>

{{output}}
<lang applescript>{997, 1699, 1789, 1879, 1987, 2689, 2797, 2887, 3499, 3697, 3769, 3877, 3967, 4597, 4759, 4957, 4993}</lang>

=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<lang AWK>