Primes whose sum of digits is 25: Difference between revisions

(Added Arturo implementation)
Line 1,326:
{{out}}
<pre>997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993 </pre>
 
=={{header|Nim}}==
===Task===
<lang Nim>import strutils, sugar
 
func isPrime(n: Natural): bool =
if n < 2: return false
if n mod 2 == 0: return n == 2
if n mod 3 == 0: return n == 3
var d = 5
while d * d <= n:
if n mod d == 0: return false
inc d, 2
if n mod d == 0: return false
inc d, 4
result = true
 
func digitSum(n: Natural): int =
var n = n
while n != 0:
result += n mod 10
n = n div 10
 
let result = collect(newSeq):
for n in countup(3, 5000, 2):
if digitSum(n) == 25 and n.isPrime: n
 
for i, n in result:
stdout.write ($n).align(4), if (i + 1) mod 6 == 0: '\n' else: ' '
echo()</lang>
 
{{out}}
<pre> 997 1699 1789 1879 1987 2689
2797 2887 3499 3697 3769 3877
3967 4597 4759 4957 4993 </pre>
 
===Stretch goal===
{{trans|Julia}}
{{libheader|bignum}}
<lang Nim>import std/monotimes, strformat, strutils
import bignum
 
func sum25(p: string; rm, res: Natural): Natural =
result = res
if rm == 0:
if p[^1] in "1379" and probablyPrime(newInt(p), 25) != 0:
inc result
else:
for i in 1..min(rm, 9):
result = sum25(p & chr(i + ord('0')), rm - i, result)
 
let t0 = getMonoTime()
let count = $sum25("", 25, 0)
echo &"There are {count.insertSep()} primes whose digits sum to 25 without any zero digits."
echo "\nExecution time: ", getMonoTime() - t0</lang>
 
{{out}}
<pre>There are 1_525_141 primes whose digits sum to 25 without any zero digits.
 
Execution time: (seconds: 12, nanosecond: 182051288)</pre>
 
=={{header|Pascal}}==
Anonymous user