Cullen and Woodall numbers: Difference between revisions

Line 839:
2 3 6 30 75 81 115 123 249 362 384 462
</pre>
 
=={{header|jq}}==
'''Works with jq and gojq, the C and Go implementations of jq'''
 
The algorithm for checking whether a number is prime is too slow for finding more than the first Cullen prime,
or more than the first four Woodall primes.
<syntaxhighlight lang=jq>
def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
else
($n | sqrt) as $rt
| 23
| until( . > $rt or ($n % . == 0); .+2)
| . > $rt
end;
 
def ipow($m; $n): reduce range(0;$n) as $i (1; $m * .);
 
def cullen: ipow(2;.) * . + 1;
 
def woodall: cullen - 2;
 
def task:
"First 20 Cullen numbers (n * 2^n + 1):",
(range(1; 21) | cullen),
"\n\nFirst 20 Woodall numbers (n * 2^n - 1):",
(range(1; 21) | woodall),
 
"\n\nFirst Cullen primes (in terms of n):",
limit(1;
range(1; infinite)
| select(cullen|is_prime) ),
 
"\n\nFirst 4 Woodall primes (in terms of n):",
limit(4;
range(0; infinite)
| select(woodall|is_prime) ) ;
 
task
</syntaxhighlight>
{{output}}
<pre>
First 20 Cullen numbers (n * 2^n + 1):
3
9
25
65
161
385
897
2049
4609
10241
22529
49153
106497
229377
491521
1048577
2228225
4718593
9961473
20971521
 
 
First 20 Woodall numbers (n * 2^n - 1):
1
7
23
63
159
383
895
2047
4607
10239
22527
49151
106495
229375
491519
1048575
2228223
4718591
9961471
20971519
 
First Cullen primes (in terms of n):
1
 
 
First 4 Woodall primes (in terms of n):
2
3
6
30
</pre>
 
 
=={{header|Julia}}==
2,458

edits