Safe and Sophie Germain primes: Difference between revisions

Created Nim solution.
No edit summary
(Created Nim solution.)
Line 487:
683 719 743 761 809 911 953 1013 1019 1031
1049 1103 1223 1229 1289 1409 1439 1451 1481 1499
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/strutils
 
func isPrime(n: Natural): bool =
if n < 2: return false
if (n and 1) == 0: return n == 2
if n mod 3 == 0: return n == 3
var k = 5
var delta = 2
while k * k <= n:
if n mod k == 0: return false
inc k, delta
delta = 6 - delta
result = true
 
iterator sophieGermainPrimes(): int =
var n = 2
while true:
if isPrime(n) and isPrime(2 * n + 1):
yield n
inc n
 
echo "First 50 Sophie Germain primes:"
var count = 0
for n in sophieGermainPrimes():
inc count
stdout.write align($n, 4)
stdout.write if count mod 10 == 0: '\n' else: ' '
if count == 50: break
</syntaxhighlight>
 
{{out}}
<pre>First 50 Sophie Germain primes:
2 3 5 11 23 29 41 53 83 89
113 131 173 179 191 233 239 251 281 293
359 419 431 443 491 509 593 641 653 659
683 719 743 761 809 911 953 1013 1019 1031
1049 1103 1223 1229 1289 1409 1439 1451 1481 1499
</pre>
 
256

edits