Additive primes: Difference between revisions

m (→‎{{header|Phix}}: fixed a couple of syntax glitches, added personal tag)
Line 536:
54 additive primes found.
</pre>
 
=={{header|Nim}}==
<lang Nim>import math, strutils
 
const N = 499
 
# Sieve of Erathostenes.
var composite: array[2..N, bool] # Initialized to false, ie. prime.
 
for n in 2..sqrt(N.toFloat).int:
if not composite[n]:
for k in countup(n * n, N, n):
composite[k] = true
 
 
func digitSum(n: Positive): Natural =
## Compute sum of digits.
var n = n.int
while n != 0:
result += n mod 10
n = n div 10
 
 
echo "Additive primes less than 500:"
var count = 0
for n in 2..N:
if not composite[n] and not composite[digitSum(n)]:
inc count
stdout.write ($n).align(3)
stdout.write if count mod 10 == 0: '\n' else: ' '
echo()
 
echo "\nNumber of additive primes found: ", count</lang>
 
{{out}}
<pre>Additive primes less than 500:
2 3 5 7 11 23 29 41 43 47
61 67 83 89 101 113 131 137 139 151
157 173 179 191 193 197 199 223 227 229
241 263 269 281 283 311 313 317 331 337
353 359 373 379 397 401 409 421 443 449
461 463 467 487
 
Number of additive primes found: 54</pre>
 
=={{header|Perl}}==
Anonymous user