Find prime n such that reversed n is also prime: Difference between revisions
Content deleted Content added
m →{{header|Phix}}: fixed a couple of syntax glitches, added personal tag |
|||
Line 425: | Line 425: | ||
</pre> |
</pre> |
||
=={{header|Nim}}== |
|||
<lang Nim>import math, strutils |
|||
const |
|||
N1 = 499 # Limit for the primes. |
|||
N2 = 999 # Limit for the reverses of primes. |
|||
# Sieve of Erathosthenes. |
|||
var composite: array[2..N2, bool] # Default is false. |
|||
for p in 2..sqrt(N2.toFloat).int: |
|||
if not composite[p]: |
|||
for k in countup(p * p, N2, p): |
|||
composite[k] = true |
|||
template isPrime(n: int): bool = not composite[n] |
|||
func reversed(n: int): int = |
|||
var n = n |
|||
while n != 0: |
|||
result = 10 * result + n mod 10 |
|||
n = n div 10 |
|||
var result: seq[int] |
|||
for n in 2..N1: |
|||
if n.isPrime and reversed(n).isPrime: |
|||
result.add n |
|||
for i, n in result: |
|||
stdout.write ($n).align(3) |
|||
stdout.write if (i + 1) mod 10 == 0: '\n' else: ' ' |
|||
echo()</lang> |
|||
{{out}} |
|||
<pre> 2 3 5 7 11 13 17 31 37 71 |
|||
73 79 97 101 107 113 131 149 151 157 |
|||
167 179 181 191 199 311 313 337 347 353 |
|||
359 373 383 389 </pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |