Cullen and Woodall numbers: Difference between revisions

Created Nim solution.
(Created Nim solution.)
Line 1,028:
{1, 141, 4713, 5795, 6611}
{2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462}</pre>
 
=={{header|Nim}}==
{{libheader|Integers}}
<syntaxhighlight lang="Nim">import std/strformat
import integers
 
iterator cullenNumbers(): (int, Integer) =
var n = 1
var p = newInteger(2)
while true:
yield (n , n * p + 1)
inc n
p = p shl 1
 
iterator woodallNumbers(): (int, Integer) =
var n = 1
var p = newInteger(2)
while true:
yield (n , n * p - 1)
inc n
p = p shl 1
 
echo "First 20 Cullen numbers:"
for (n, cn) in cullenNumbers():
stdout.write &"{cn:>9}"
if n mod 5 == 0: echo()
if n == 20: break
 
echo "\nFirst 20 Woodall numbers:"
for (n, wn) in woodallNumbers():
stdout.write &"{wn:>9}"
if n mod 5 == 0: echo()
if n == 20: break
 
echo "\nFirst 5 Cullen primes (in terms of n):"
var count = 0
for (n, cn) in cullenNumbers():
if cn.isPrime:
stdout.write ' ', n
inc count
if count == 5: break
echo()
 
echo "\nFirst 12 Woodall primes (in terms of n):"
count = 0
for (n, wn) in woodallNumbers():
if wn.isPrime:
stdout.write ' ', n
inc count
if count == 12: break
echo()
</syntaxhighlight>
 
{{out}}
<pre>First 20 Cullen numbers:
3 9 25 65 161
385 897 2049 4609 10241
22529 49153 106497 229377 491521
1048577 2228225 4718593 9961473 20971521
 
First 20 Woodall numbers:
1 7 23 63 159
383 895 2047 4607 10239
22527 49151 106495 229375 491519
1048575 2228223 4718591 9961471 20971519
 
First 5 Cullen primes (in terms of n):
1 141 4713 5795 6611
 
First 12 Woodall primes (in terms of n):
2 3 6 30 75 81 115 123 249 362 384 462
</pre>
 
=={{header|Perl}}==
256

edits