Primes whose sum of digits is 25: Difference between revisions

Content added Content deleted
(Add Tcl)
m (→‎{{header|AppleScript}}: →‎Idiomatic: Digit-summing code to own handler. Tidied)
Line 347: Line 347:
end repeat
end repeat
repeat with position from 2 to (limit ^ 0.5) div 1
repeat with n from 2 to (limit ^ 0.5) div 1
if (item position of o's numberList is not missing value) then
if (item n of o's numberList is n) then
repeat with multiple from position * position to limit by position
repeat with multiple from n * n to limit by n
set item multiple of o's numberList to missing value
set item multiple of o's numberList to missing value
end repeat
end repeat
Line 357: Line 357:
return o's numberList's numbers
return o's numberList's numbers
end sieveOfEratosthenes
end sieveOfEratosthenes

on sumOfDigits(n) -- n assumed to be a positive decimal integer.
set sum to n mod 10
set n to n div 10
repeat until (n = 0)
set sum to sum + n mod 10
set n to n div 10
end repeat
return sum
end sumOfDigits


on numbersWhoseDigitsSumTo(numList, targetSum)
on numbersWhoseDigitsSumTo(numList, targetSum)
Line 364: Line 375:
end script
end script
repeat with thisNumber in o's numberList
repeat with n in o's numberList
set n to thisNumber's contents
if (sumOfDigits(n) = targetSum) then set end of o's output to n'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
end repeat
Line 378: Line 383:


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


{{output}}
{{output}}