Cullen and Woodall numbers: Difference between revisions
Added Algol 68 |
Add Scala implementation |
||
(54 intermediate revisions by 27 users not shown) | |||
Line 1: | Line 1: | ||
{{ |
{{task}} |
||
A [[wp:Cullen_number|Cullen number]] is a number of the form <span style="font-size:125%;">'''n × 2<sup>n</sup> + 1'''</span> where <span style="font-size:125%;">'''n'''</span> is a natural number. |
A [[wp:Cullen_number|Cullen number]] is a number of the form <span style="font-size:125%;">'''n × 2<sup>n</sup> + 1'''</span> where <span style="font-size:125%;">'''n'''</span> is a natural number. |
||
Line 39: | Line 39: | ||
* [[oeis:A002234|OEIS:A002234 - Numbers k such that the Woodall number k*2^k - 1 is prime]] |
* [[oeis:A002234|OEIS:A002234 - Numbers k such that the Woodall number k*2^k - 1 is prime]] |
||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="11l"> |
|||
F cullen(n) |
|||
R (n << n) + 1 |
|||
F woodall(n) |
|||
R (n << n) - 1 |
|||
print(‘First 20 Cullen numbers:’) |
|||
L(i) 1..20 |
|||
print(cullen(i), end' ‘ ’) |
|||
print() |
|||
print() |
|||
print(‘First 20 Woodall numbers:’) |
|||
L(i) 1..20 |
|||
print(woodall(i), end' ‘ ’) |
|||
print() |
|||
</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 |
|||
</pre> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 44: | Line 74: | ||
Uses Algol 68Gs LONG LONG INT for long integers. The number of digits must be specified and appears to affect the run time as larger sies are specified. This sample only shows the first two Cullen primes as the time taken to find the third is rather long. |
Uses Algol 68Gs LONG LONG INT for long integers. The number of digits must be specified and appears to affect the run time as larger sies are specified. This sample only shows the first two Cullen primes as the time taken to find the third is rather long. |
||
{{libheader|ALGOL 68-primes}} |
{{libheader|ALGOL 68-primes}} |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find Cullen and Woodall numbers and determine which are prime # |
||
# a Cullen number n is n2^2 + 1, Woodall number is n2^n - 1 # |
# a Cullen number n is n2^2 + 1, Woodall number is n2^n - 1 # |
||
PR read "primes.incl.a68" PR # include prime utilities # |
PR read "primes.incl.a68" PR # include prime utilities # |
||
Line 93: | Line 123: | ||
print( ( newline ) ) |
print( ( newline ) ) |
||
END |
END |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 101: | Line 131: | ||
Index of the 1st 12 Woodall primes: 2 3 6 30 75 81 115 123 249 362 384 462 |
Index of the 1st 12 Woodall primes: 2 3 6 30 75 81 115 123 249 362 384 462 |
||
</pre> |
</pre> |
||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">cullen: function [n]-> |
|||
inc n * 2^n |
|||
woodall: function [n]-> |
|||
dec n * 2^n |
|||
print ["First 20 cullen numbers:" join.with:" " to [:string] map 1..20 => cullen] |
|||
print ["First 20 woodall numbers:" join.with:" " to [:string] map 1..20 => woodall]</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</pre> |
|||
=={{header|Asymptote}}== |
|||
<syntaxhighlight lang="Asymptote">int num = 0; |
|||
write("First 20 Cullen numbers:"); |
|||
for(int n = 1; n < 20; ++n) { |
|||
num = n * (2^n) + 1; |
|||
write(" ", num, suffix=none); |
|||
} |
|||
write(""); |
|||
write("First 20 Woodall numbers:"); |
|||
for(int n = 1; n < 20; ++n) { |
|||
num = n * (2^n) - 1; |
|||
write(" ", num, suffix=none); |
|||
}</syntaxhighlight> |
|||
=={{header|AWK}}== |
|||
<syntaxhighlight lang="awk"> |
|||
# syntax: GAWK -f CULLEN_AND_WOODALL_NUMBERS.AWK |
|||
BEGIN { |
|||
start = 1 |
|||
stop = 20 |
|||
printf("Cullen %d-%d:",start,stop) |
|||
for (n=start; n<=stop; n++) { |
|||
printf(" %d",n*(2^n)+1) |
|||
} |
|||
printf("\n") |
|||
printf("Woodall %d-%d:",start,stop) |
|||
for (n=start; n<=stop; n++) { |
|||
printf(" %d",n*(2^n)-1) |
|||
} |
|||
printf("\n") |
|||
exit(0) |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Cullen 1-20: 3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 |
|||
Woodall 1-20: 1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 |
|||
</pre> |
|||
=={{header|BASIC}}== |
|||
==={{header|Applesoft BASIC}}=== |
|||
{{works with|Chipmunk Basic}} |
|||
{{trans|Chipmunk Basic}} |
|||
<syntaxhighlight lang="qbasic">10 REM Cullen and Woodall numbers |
|||
20 HOME : REM 20 CLS for Chipmunk Basic |
|||
30 PRINT "First 20 Cullen numbers:" |
|||
40 FOR n = 1 TO 20 |
|||
50 num = n*(2^n)+1 |
|||
60 PRINT INT(num);" "; |
|||
70 NEXT |
|||
80 PRINT : PRINT |
|||
90 PRINT "First 20 Woodall numbers:" |
|||
100 FOR n = 1 TO 20 |
|||
110 num = n*(2^n)-1 |
|||
120 PRINT INT(num);" "; |
|||
130 NEXT n |
|||
140 END</syntaxhighlight> |
|||
==={{header|BASIC256}}=== |
|||
{{works with|Run BASIC}} |
|||
{{works with|Just BASIC}} |
|||
{{works with|Liberty BASIC}} |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="basic256">print "First 20 Cullen numbers:" |
|||
for n = 1 to 20 |
|||
num = n * (2^n)+1 |
|||
print int(num); " "; |
|||
next |
|||
print : print |
|||
print "First 20 Woodall numbers:" |
|||
for n = 1 to 20 |
|||
num = n * (2^n)-1 |
|||
print int(num); " "; |
|||
next n |
|||
end</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Igual que la entrada de FreeBASIC.</pre> |
|||
==={{header|Chipmunk Basic}}=== |
|||
{{works with|Chipmunk Basic|3.6.4}} |
|||
{{works with|Applesoft BASIC}} |
|||
{{works with|MSX_BASIC}} |
|||
{{works with|PC-BASIC|any}} |
|||
{{works with|QBasic}} |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="qbasic">10 REM Cullen and Woodall numbers |
|||
20 CLS : REM 20 HOME for Applesoft BASIC |
|||
30 PRINT "First 20 Cullen numbers:" |
|||
40 FOR n = 1 TO 20 |
|||
50 num = n*(2^n)+1 |
|||
60 PRINT INT(num);" "; |
|||
70 NEXT |
|||
80 PRINT : PRINT |
|||
90 PRINT "First 20 Woodall numbers:" |
|||
100 FOR n = 1 TO 20 |
|||
110 num = n*(2^n)-1 |
|||
120 PRINT INT(num);" "; |
|||
130 NEXT n |
|||
140 END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
==={{header|FreeBASIC}}=== |
|||
<syntaxhighlight lang="vb">Dim As Uinteger n, num |
|||
Print "First 20 Cullen numbers:" |
|||
For n = 1 To 20 |
|||
num = n * (2^n)+1 |
|||
Print num; " "; |
|||
Next |
|||
Print !"\n\nFirst 20 Woodall numbers:" |
|||
For n = 1 To 20 |
|||
num = n * (2^n)-1 |
|||
Print num; " "; |
|||
Next n |
|||
Sleep</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</pre> |
|||
==={{header|Gambas}}=== |
|||
<syntaxhighlight lang="vbnet">Public Sub Main() |
|||
Dim n, num As Integer |
|||
Print "First 20 Cullen numbers:" |
|||
For n = 1 To 20 |
|||
num = n * (2 ^ n) + 1 |
|||
Print num; " "; |
|||
Next |
|||
Print "\n\nFirst 20 Woodall numbers:" |
|||
For n = 1 To 20 |
|||
num = n * (2 ^ n) - 1 |
|||
Print num; " "; |
|||
Next |
|||
End </syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
==={{header|GW-BASIC}}=== |
|||
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes. |
|||
==={{header|PureBasic}}=== |
|||
<syntaxhighlight lang="purebasic">OpenConsole() |
|||
PrintN("First 20 Cullen numbers:") |
|||
For n.i = 1 To 20 |
|||
num = n * Pow(2, n)+1 |
|||
Print(Str(num) + " ") |
|||
Next |
|||
PrintN(#CRLF$ + "First 20 Woodall numbers:") |
|||
For n.i = 1 To 20 |
|||
num = n * Pow(2, n)-1 |
|||
Print(Str(num) + " ") |
|||
Next n |
|||
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input() |
|||
CloseConsole()</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Igual que la entrada de FreeBASIC.</pre> |
|||
==={{header|QBasic}}=== |
|||
{{works with|QBasic|1.1}} |
|||
{{works with|QuickBasic|4.5}} |
|||
{{works with|True BASIC}} |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="qbasic">DIM num AS LONG ''comment this line for True BASIC |
|||
PRINT "First 20 Cullen numbers:" |
|||
FOR n = 1 TO 20 |
|||
LET num = n * (2 ^ n) + 1 |
|||
PRINT num; |
|||
NEXT n |
|||
PRINT |
|||
PRINT |
|||
PRINT "First 20 Woodall numbers:" |
|||
FOR n = 1 TO 20 |
|||
LET num = n * (2 ^ n) - 1 |
|||
PRINT num; |
|||
NEXT n |
|||
END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Igual que la entrada de FreeBASIC.</pre> |
|||
==={{header|Run BASIC}}=== |
|||
{{works with|BASIC256}} |
|||
{{works with|Just BASIC}} |
|||
{{works with|Liberty BASIC}} |
|||
<syntaxhighlight lang="vb">print "First 20 Cullen numbers:" |
|||
for n = 1 to 20 |
|||
num = n * (2^n)+1 |
|||
print int(num); " "; |
|||
next |
|||
print : print |
|||
print "First 20 Woodall numbers:" |
|||
for n = 1 to 20 |
|||
num = n * (2^n)-1 |
|||
print int(num); " "; |
|||
next n |
|||
end</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
==={{Header|Tiny BASIC}}=== |
|||
<syntaxhighlight lang="qbasic">REM Rosetta Code problem: https://rosettacode.org/wiki/Cullen_and_Woodall_numbers |
|||
REM by Jjuanhdez, 03/2023 |
|||
REM TinyBasic does not support values greater than 32767 |
|||
PRINT "First 11 Cullen numbers:" |
|||
LET N = 0 |
|||
LET I = 1 |
|||
10 IF I = 12 THEN GOTO 20 |
|||
GOSUB 50 |
|||
LET N = (I*R) +1 |
|||
PRINT N, " " |
|||
LET I = I+1 |
|||
GOTO 10 |
|||
20 PRINT "" |
|||
PRINT "First 11 Woodall numbers:" |
|||
LET I = 1 |
|||
30 IF I = 12 THEN GOTO 40 |
|||
GOSUB 50 |
|||
LET N = (I*R) -1 |
|||
PRINT N, " " |
|||
LET I = I+1 |
|||
GOTO 30 |
|||
40 END |
|||
50 REM Exponent calculation |
|||
LET A = 2 |
|||
LET B = I |
|||
LET X = 1 |
|||
LET R = 2 |
|||
60 IF X >= B THEN RETURN |
|||
LET T = R |
|||
IF R < A THEN LET R = A*A |
|||
IF T < A THEN GOTO 70 |
|||
IF R >= A THEN LET R = R*A |
|||
70 LET X = X+1 |
|||
GOTO 60</syntaxhighlight> |
|||
{{out}} |
|||
<pre>First 11 Cullen numbers: |
|||
3 |
|||
9 |
|||
25 |
|||
65 |
|||
161 |
|||
385 |
|||
897 |
|||
2049 |
|||
4609 |
|||
10241 |
|||
22529 |
|||
First 11 Woodall numbers: |
|||
1 |
|||
7 |
|||
23 |
|||
63 |
|||
159 |
|||
383 |
|||
895 |
|||
2047 |
|||
4607 |
|||
10239 |
|||
22527</pre> |
|||
==={{header|True BASIC}}=== |
|||
{{works with|QBasic}} |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="qbasic">REM DIM num AS LONG !uncomment this line for QBasic |
|||
PRINT "First 20 Cullen numbers:" |
|||
FOR n = 1 TO 20 |
|||
LET num = n * (2 ^ n) + 1 |
|||
PRINT num; |
|||
NEXT n |
|||
PRINT |
|||
PRINT |
|||
PRINT "First 20 Woodall numbers:" |
|||
FOR n = 1 TO 20 |
|||
LET num = n * (2 ^ n) - 1 |
|||
PRINT num; |
|||
NEXT n |
|||
END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Igual que la entrada de FreeBASIC.</pre> |
|||
==={{header|MSX Basic}}=== |
|||
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes. |
|||
==={{header|XBasic}}=== |
|||
{{works with|Windows XBasic}} |
|||
<syntaxhighlight lang="qbasic">PROGRAM "progname" |
|||
VERSION "0.0000" |
|||
IMPORT "xma" |
|||
DECLARE FUNCTION Entry () |
|||
FUNCTION Entry () |
|||
PRINT "First 20 Cullen numbers:" |
|||
FOR n = 1 TO 20 |
|||
num! = n * POWER (2, n) + 1 |
|||
PRINT num!; |
|||
NEXT n |
|||
PRINT |
|||
PRINT |
|||
PRINT "First 20 Woodall numbers:" |
|||
FOR n = 1 TO 20 |
|||
num! = n * POWER (2, n) - 1 |
|||
PRINT num!; |
|||
NEXT n |
|||
END FUNCTION |
|||
END PROGRAM</syntaxhighlight> |
|||
==={{header|Yabasic}}=== |
|||
<syntaxhighlight lang="basic">print "First 20 Cullen numbers:" |
|||
for n = 1 to 20 |
|||
num = n * (2^n)+1 |
|||
print num, " "; |
|||
next |
|||
print "\n\nFirst 20 Woodall numbers:" |
|||
for n = 1 to 20 |
|||
num = n * (2^n)-1 |
|||
print num, " "; |
|||
next n |
|||
print |
|||
end</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Igual que la entrada de FreeBASIC.</pre> |
|||
=={{header|C++}}== |
|||
<syntaxhighlight lang="c++"> |
|||
#include <cstdint> |
|||
#include <iostream> |
|||
#include <string> |
|||
uint32_t number, power; |
|||
void number_initialise() { |
|||
number = 0; |
|||
power = 1; |
|||
} |
|||
enum NumberType { Cullen, Woodhall }; |
|||
uint32_t next_number(const NumberType& number_type) { |
|||
number += 1; |
|||
power <<= 1; |
|||
switch ( number_type ) { |
|||
case Cullen: return number * power + 1; |
|||
case Woodhall: return number * power - 1; |
|||
}; |
|||
return 0; |
|||
} |
|||
void number_sequence(const uint32_t& count, const NumberType& number_type) { |
|||
std::string type = ( number_type == Cullen ) ? "Cullen" : "Woodhall"; |
|||
std::cout << "The first " << count << " " << type << " numbers are:" << std::endl; |
|||
number_initialise(); |
|||
for ( uint32_t index = 1; index <= count; ++index ) { |
|||
std::cout << next_number(number_type) << " "; |
|||
} |
|||
std::cout << std::endl << std::endl; |
|||
} |
|||
int main() { |
|||
number_sequence(20, Cullen); |
|||
number_sequence(20, Woodhall); |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
The first 20 Cullen numbers are: |
|||
3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 |
|||
The first 20 Woodhall numbers are: |
|||
1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 |
|||
</pre> |
|||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
<syntaxhighlight lang="Delphi"> |
|||
uses SysUtils,StdCtrls; |
|||
procedure CullenWoodallTest(Memo: TMemo); |
|||
implementation |
|||
procedure FindCullenNumbers(Memo: TMemo); |
|||
var N,R: integer; |
|||
var S: string; |
|||
begin |
|||
S:=''; |
|||
Memo.Lines.Add('First 20 Cullen Numbers:'); |
|||
for N:=1 to 20 do |
|||
begin |
|||
R:=N * (1 shl N) + 1; |
|||
S:=S+IntToStr(R)+' '; |
|||
end; |
|||
Memo.Lines.Add(S); |
|||
end; |
|||
procedure FindWoodallNumbers(Memo: TMemo); |
|||
var N,R: integer; |
|||
var S: string; |
|||
begin |
|||
S:=''; |
|||
Memo.Lines.Add('First 20 Woodall Numbers:'); |
|||
for N:=1 to 20 do |
|||
begin |
|||
R:=N * (1 shl N) - 1; |
|||
S:=S+IntToStr(R)+' '; |
|||
end; |
|||
Memo.Lines.Add(S); |
|||
end; |
|||
procedure CullenWoodallTest(Memo: TMemo); |
|||
begin |
|||
FindCullenNumbers(Memo); |
|||
FindWoodallNumbers(Memo); |
|||
end; |
|||
</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 |
|||
</pre> |
|||
=={{header|EasyLang}}== |
|||
<syntaxhighlight lang="easylang"> |
|||
for n = 1 to 20 |
|||
write n * pow 2 n + 1 & " " |
|||
. |
|||
print "" |
|||
for n = 1 to 20 |
|||
write n * pow 2 n - 1 & " " |
|||
. |
|||
print "" |
|||
</syntaxhighlight> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Cullen and Woodall numbers. Nigel Galloway: January 14th., 2022 |
// Cullen and Woodall numbers. Nigel Galloway: January 14th., 2022 |
||
let Cullen,Woodall=let fG n (g:int)=(bigint g)*2I**g+n in fG 1I, fG -1I |
let Cullen,Woodall=let fG n (g:int)=(bigint g)*2I**g+n in fG 1I, fG -1I |
||
Line 110: | Line 640: | ||
Seq.initInfinite((+)1)|>Seq.filter(fun n->let mutable n=Woodall n in Open.Numeric.Primes.MillerRabin.IsProbablePrime &n)|>Seq.take 12|>Seq.iter(printf "%A "); printfn "" |
Seq.initInfinite((+)1)|>Seq.filter(fun n->let mutable n=Woodall n in Open.Numeric.Primes.MillerRabin.IsProbablePrime &n)|>Seq.take 12|>Seq.iter(printf "%A "); printfn "" |
||
Seq.initInfinite((+)1)|>Seq.filter(fun n->let mutable n=Cullen n in Open.Numeric.Primes.MillerRabin.IsProbablePrime &n)|>Seq.take 5|>Seq.iter(printf "%A "); printfn "" |
Seq.initInfinite((+)1)|>Seq.filter(fun n->let mutable n=Cullen n in Open.Numeric.Primes.MillerRabin.IsProbablePrime &n)|>Seq.take 5|>Seq.iter(printf "%A "); printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 118: | Line 648: | ||
1 141 4713 5795 6611 |
1 141 4713 5795 6611 |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
|||
{{works with|Factor|0.99 2022-04-03}} |
|||
<syntaxhighlight lang="factor">USING: arrays kernel math math.vectors prettyprint ranges |
|||
sequences ; |
|||
20 [1..b] [ dup 2^ * 1 + ] map dup 2 v-n 2array simple-table.</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 |
|||
1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 |
|||
</pre> |
|||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
local fn CullenAndWoodall( limit as long ) |
|||
NSUInteger i, cullen, woodall |
|||
printf @"%13s %9s", fn StringUTF8String( @"Cullen" ), fn StringUTF8String( @"Woodall" ) |
|||
for i = 1 to limit |
|||
cullen = i * ( 2^i ) + 1 |
|||
woodall = i * ( 2^i ) - 1 |
|||
printf @"%3lu %9lu %9lu", i, cullen, woodall |
|||
next |
|||
end fn |
|||
fn CullenAndWoodall( 20 ) |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
Cullen Woodall |
|||
1 3 1 |
|||
2 9 7 |
|||
3 25 23 |
|||
4 65 63 |
|||
5 161 159 |
|||
6 385 383 |
|||
7 897 895 |
|||
8 2049 2047 |
|||
9 4609 4607 |
|||
10 10241 10239 |
|||
11 22529 22527 |
|||
12 49153 49151 |
|||
13 106497 106495 |
|||
14 229377 229375 |
|||
15 491521 491519 |
|||
16 1048577 1048575 |
|||
17 2228225 2228223 |
|||
18 4718593 4718591 |
|||
19 9961473 9961471 |
|||
20 20971521 20971519 |
|||
</pre> |
|||
=={{header|Go}}== |
|||
{{libheader|GMP(Go wrapper)}} |
|||
<syntaxhighlight lang="go">package main |
|||
import ( |
|||
"fmt" |
|||
big "github.com/ncw/gmp" |
|||
) |
|||
func cullen(n uint) *big.Int { |
|||
one := big.NewInt(1) |
|||
bn := big.NewInt(int64(n)) |
|||
res := new(big.Int).Lsh(one, n) |
|||
res.Mul(res, bn) |
|||
return res.Add(res, one) |
|||
} |
|||
func woodall(n uint) *big.Int { |
|||
res := cullen(n) |
|||
return res.Sub(res, big.NewInt(2)) |
|||
} |
|||
func main() { |
|||
fmt.Println("First 20 Cullen numbers (n * 2^n + 1):") |
|||
for n := uint(1); n <= 20; n++ { |
|||
fmt.Printf("%d ", cullen(n)) |
|||
} |
|||
fmt.Println("\n\nFirst 20 Woodall numbers (n * 2^n - 1):") |
|||
for n := uint(1); n <= 20; n++ { |
|||
fmt.Printf("%d ", woodall(n)) |
|||
} |
|||
fmt.Println("\n\nFirst 5 Cullen primes (in terms of n):") |
|||
count := 0 |
|||
for n := uint(1); count < 5; n++ { |
|||
cn := cullen(n) |
|||
if cn.ProbablyPrime(15) { |
|||
fmt.Printf("%d ", n) |
|||
count++ |
|||
} |
|||
} |
|||
fmt.Println("\n\nFirst 12 Woodall primes (in terms of n):") |
|||
count = 0 |
|||
for n := uint(1); count < 12; n++ { |
|||
cn := woodall(n) |
|||
if cn.ProbablyPrime(15) { |
|||
fmt.Printf("%d ", n) |
|||
count++ |
|||
} |
|||
} |
|||
fmt.Println() |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<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 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|Haskell}}== |
|||
<syntaxhighlight lang="haskell">findCullen :: Int -> Integer |
|||
findCullen n = toInteger ( n * 2 ^ n + 1 ) |
|||
cullens :: [Integer] |
|||
cullens = map findCullen [1 .. 20] |
|||
woodalls :: [Integer] |
|||
woodalls = map (\i -> i - 2 ) cullens |
|||
main :: IO ( ) |
|||
main = do |
|||
putStrLn "First 20 Cullen numbers:" |
|||
print cullens |
|||
putStrLn "First 20 Woodall numbers:" |
|||
print woodalls</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] |
|||
</pre> |
|||
=={{header|J}}== |
|||
<syntaxhighlight lang="j">cullen=: {{ y* 1+2x^y }} |
|||
woodall=: {{ y*_1+2x^y }}</syntaxhighlight> |
|||
Task example: |
|||
<syntaxhighlight lang="j"> cullen 1+i.20 |
|||
3 10 27 68 165 390 903 2056 4617 10250 22539 49164 106509 229390 491535 1048592 2228241 4718610 9961491 20971540 |
|||
woodall 1+i.20 |
|||
1 6 21 60 155 378 889 2040 4599 10230 22517 49140 106483 229362 491505 1048560 2228207 4718574 9961453 20971500</syntaxhighlight> |
|||
=={{header|Java}}== |
|||
<syntaxhighlight lang="java"> |
|||
import java.math.BigInteger; |
|||
public final class CullenAndWoodhall { |
|||
public static void main(String[] aArgs) { |
|||
numberSequence(20, NumberType.Cullen); |
|||
numberSequence(20, NumberType.Woodhall); |
|||
primeSequence(5, NumberType.Cullen); |
|||
primeSequence(12, NumberType.Woodhall); |
|||
} |
|||
private enum NumberType { Cullen, Woodhall } |
|||
private static void numberSequence(int aCount, NumberType aNumberType) { |
|||
System.out.println(); |
|||
System.out.println("The first " + aCount + " " + aNumberType + " numbers are:"); |
|||
numberInitialise(); |
|||
for ( int index = 1; index <= aCount; index++ ) { |
|||
System.out.print(nextNumber(aNumberType) + " "); |
|||
} |
|||
System.out.println(); |
|||
} |
|||
private static void primeSequence(int aCount, NumberType aNumberType) { |
|||
System.out.println(); |
|||
System.out.println("The indexes of the first " + aCount + " " + aNumberType + " primes are:"); |
|||
primeInitialise(); |
|||
while ( count < aCount ) { |
|||
if ( nextNumber(aNumberType).isProbablePrime(CERTAINTY) ) { |
|||
System.out.print(primeIndex + " "); |
|||
count += 1; |
|||
} |
|||
primeIndex += 1; |
|||
} |
|||
System.out.println(); |
|||
} |
|||
private static BigInteger nextNumber(NumberType aNumberType) { |
|||
number = number.add(BigInteger.ONE); |
|||
power = power.shiftLeft(1); |
|||
return switch ( aNumberType ) { |
|||
case Cullen -> number.multiply(power).add(BigInteger.ONE); |
|||
case Woodhall -> number.multiply(power).subtract(BigInteger.ONE); |
|||
}; |
|||
} |
|||
private static void numberInitialise() { |
|||
number = BigInteger.ZERO; |
|||
power = BigInteger.ONE; |
|||
} |
|||
private static void primeInitialise() { |
|||
count = 0; |
|||
primeIndex = 1; |
|||
numberInitialise(); |
|||
} |
|||
private static BigInteger number; |
|||
private static BigInteger power; |
|||
private static int count; |
|||
private static int primeIndex; |
|||
private static final int CERTAINTY = 20; |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
The first 20 Cullen numbers are: |
|||
3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 |
|||
The first 20 Woodhall numbers are: |
|||
1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 |
|||
The indexes of the first 5 Cullen primes are: |
|||
1 141 4713 5794 6611 |
|||
The indexes of the first 12 Woodhall primes are: |
|||
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}}== |
=={{header|Julia}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="julia">using Lazy |
||
using Primes |
using Primes |
||
Line 133: | Line 1,020: | ||
println("\nFirst 5 Cullen primes: (in terms of n)\n", take(5, primecullens)) # A005849 |
println("\nFirst 5 Cullen primes: (in terms of n)\n", take(5, primecullens)) # A005849 |
||
println("\nFirst 12 Woodall primes: (in terms of n)\n", Int.(collect(take(12, primewoodalls)))) # A002234 |
println("\nFirst 12 Woodall primes: (in terms of n)\n", Int.(collect(take(12, primewoodalls)))) # A002234 |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
First 20 Cullen numbers: ( n × 2**n + 1) |
First 20 Cullen numbers: ( n × 2**n + 1) |
||
Line 146: | Line 1,033: | ||
[2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462] |
[2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462] |
||
</pre> |
</pre> |
||
=={{header|Lua}}== |
|||
<syntaxhighlight lang="lua">function T(t) return setmetatable(t, {__index=table}) end |
|||
table.range = function(t,n) local s=T{} for i=1,n do s[i]=i end return s end |
|||
table.map = function(t,f) local s=T{} for i=1,#t do s[i]=f(t[i]) end return s end |
|||
function cullen(n) return (n<<n)+1 end |
|||
print("First 20 Cullen numbers:") |
|||
print(T{}:range(20):map(cullen):concat(" ")) |
|||
function woodall(n) return (n<<n)-1 end |
|||
print("First 20 Woodall numbers:") |
|||
print(T{}:range(20):map(woodall):concat(" "))</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</pre> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">ClearAll[CullenNumber, WoodallNumber] |
|||
SetAttributes[{CullenNumber, WoodallNumber}, Listable] |
|||
CullenNumber[n_Integer] := n 2^n + 1 |
|||
WoodallNumber[n_Integer] := n 2^n - 1 |
|||
CullenNumber[Range[20]] |
|||
WoodallNumber[Range[20]] |
|||
cps = {}; |
|||
Do[ |
|||
If[PrimeQ[CullenNumber[i]], |
|||
AppendTo[cps, i]; |
|||
If[Length[cps] >= 5, Break[]] |
|||
] |
|||
, |
|||
{i, 1, \[Infinity]} |
|||
] |
|||
cps |
|||
wps = {}; |
|||
Do[ |
|||
If[PrimeQ[WoodallNumber[i]], |
|||
AppendTo[wps, i]; |
|||
If[Length[wps] >= 12, Break[]] |
|||
] |
|||
, |
|||
{i, 1, \[Infinity]} |
|||
]; |
|||
wps</syntaxhighlight> |
|||
{{out}} |
|||
<pre>{3, 9, 25, 65, 161, 385, 897, 2049, 4609, 10241, 22529, 49153, 106497, 229377, 491521, 1048577, 2228225, 4718593, 9961473, 20971521} |
|||
{1, 7, 23, 63, 159, 383, 895, 2047, 4607, 10239, 22527, 49151, 106495, 229375, 491519, 1048575, 2228223, 4718591, 9961471, 20971519} |
|||
{1, 141, 4713, 5795, 6611} |
|||
{2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462}</pre> |
|||
=={{header|Maxima}}== |
|||
<syntaxhighlight lang="maxima"> |
|||
/* Functions */ |
|||
cullen(n):=(n*2^n)+1$ |
|||
woodall(n):=(n*2^n)-1$ |
|||
/* Test cases */ |
|||
makelist(cullen(i),i,20); |
|||
makelist(woodall(i),i,20); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[3,9,25,65,161,385,897,2049,4609,10241,22529,49153,106497,229377,491521,1048577,2228225,4718593,9961473,20971521] |
|||
[1,7,23,63,159,383,895,2047,4607,10239,22527,49151,106495,229375,491519,1048575,2228223,4718591,9961471,20971519] |
|||
</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|PARI/GP}}== |
|||
<syntaxhighlight lang="PARI/GP"> |
|||
/* Define the Cullen and Woodall number functions */ |
|||
cullen(n) = n * 2^n + 1; |
|||
woodall(n) = n * 2^n - 1; |
|||
{ |
|||
/* Generate the first 20 Cullen and Woodall numbers */ |
|||
print(vector(20, n, cullen(n))); |
|||
print(vector(20, n, woodall(n))); |
|||
/* Find the first 5 Cullen prime numbers */ |
|||
cps = []; |
|||
for(i = 1, +oo, |
|||
if(isprime(cullen(i)), |
|||
cps = concat(cps, i); |
|||
if(#cps >= 5, break); |
|||
); |
|||
); |
|||
print(cps); |
|||
/* Find the first 12 Woodall prime numbers */ |
|||
wps = []; |
|||
for(i = 1, +oo, |
|||
if(isprime(woodall(i)), |
|||
wps = concat(wps, i); |
|||
if(#wps >= 12, break); |
|||
); |
|||
); |
|||
print(wps); |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[3, 9, 25, 65, 161, 385, 897, 2049, 4609, 10241, 22529, 49153, 106497, 229377, 491521, 1048577, 2228225, 4718593, 9961473, 20971521] |
|||
[1, 7, 23, 63, 159, 383, 895, 2047, 4607, 10239, 22527, 49151, 106495, 229375, 491519, 1048575, 2228223, 4718591, 9961471, 20971519] |
|||
[1, 141, 4713, 5795, 6611] |
|||
[2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462] |
|||
</pre> |
|||
=={{header|Perl}}== |
|||
{{libheader|ntheory}} |
|||
<syntaxhighlight lang="perl">use strict; |
|||
use warnings; |
|||
use bigint; |
|||
use ntheory 'is_prime'; |
|||
use constant Inf => 1e10; |
|||
sub cullen { |
|||
my($n,$c) = @_; |
|||
($n * 2**$n) + $c; |
|||
} |
|||
my($m,$n); |
|||
($m,$n) = (20,0); |
|||
print "First $m Cullen numbers:\n"; |
|||
print do { $n < $m ? (++$n and cullen($_,1) . ' ') : last } for 1 .. Inf; |
|||
($m,$n) = (20,0); |
|||
print "\n\nFirst $m Woodall numbers:\n"; |
|||
print do { $n < $m ? (++$n and cullen($_,-1) . ' ') : last } for 1 .. Inf; |
|||
($m,$n) = (5,0); |
|||
print "\n\nFirst $m Cullen primes: (in terms of n)\n"; |
|||
print do { $n < $m ? (!!is_prime(cullen $_,1) and ++$n and "$_ ") : last } for 1 .. Inf; |
|||
($m,$n) = (12,0); |
|||
print "\n\nFirst $m Woodall primes: (in terms of n)\n"; |
|||
print do { $n < $m ? (!!is_prime(cullen $_,-1) and ++$n and "$_ ") : last } for 1 .. Inf;</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|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span> |
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span> |
||
Line 197: | Line 1,311: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 12 Woodall primes (in terms of n):%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 12 Woodall primes (in terms of n):%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">)})</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 207: | Line 1,321: | ||
</pre> |
</pre> |
||
Note the time given is for desktop/Phix 64bit, for comparison the Julia entry took about 20s on the same box. On 32-bit it is nearly 5 times slower (2 minutes and 38s) and hence under pwa/p2js in a browser (which is inherently 32bit) it is limited to the first 2 cullen primes only, but manages that in 0.4s. |
Note the time given is for desktop/Phix 64bit, for comparison the Julia entry took about 20s on the same box. On 32-bit it is nearly 5 times slower (2 minutes and 38s) and hence under pwa/p2js in a browser (which is inherently 32bit) it is limited to the first 2 cullen primes only, but manages that in 0.4s. |
||
=={{header|Python}}== |
|||
<syntaxhighlight lang="python"> |
|||
print("working...") |
|||
print("First 20 Cullen numbers:") |
|||
for n in range(1,21): |
|||
num = n*pow(2,n)+1 |
|||
print(str(num),end= " ") |
|||
print() |
|||
print("First 20 Woodall numbers:") |
|||
for n in range(1,21): |
|||
num = n*pow(2,n)-1 |
|||
print(str(num),end=" ") |
|||
print() |
|||
print("done...") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
working... |
|||
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 |
|||
done... |
|||
</pre> |
|||
===Bit Shift=== |
|||
{{trans|Quackery}} |
|||
<syntaxhighlight lang="python">def cullen(n): return((n<<n)+1) |
|||
def woodall(n): return((n<<n)-1) |
|||
print("First 20 Cullen numbers:") |
|||
for i in range(1,21): |
|||
print(cullen(i),end=" ") |
|||
print() |
|||
print() |
|||
print("First 20 Woodall numbers:") |
|||
for i in range(1,21): |
|||
print(woodall(i),end=" ") |
|||
print()</syntaxhighlight> |
|||
{{out}} |
|||
Same as Quackery. |
|||
=={{header|Quackery}}== |
|||
<syntaxhighlight lang="quackery"> [ dup << 1+ ] is cullen ( n --> n ) |
|||
[ dup << 1 - ] is woodall ( n --> n ) |
|||
say "First 20 Cullen numbers:" cr |
|||
20 times [ i^ 1+ cullen echo sp ] cr |
|||
cr |
|||
say "First 20 Woodall numbers:" cr |
|||
20 times [ i^ 1+ woodall echo sp ] cr</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 |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>my @cullen = ^∞ .map: { $_ × 1 +< $_ + 1 }; |
||
my @woodall = ^∞ .map: { $_ × 1 +< $_ - 1 }; |
my @woodall = ^∞ .map: { $_ × 1 +< $_ - 1 }; |
||
Line 216: | Line 1,398: | ||
put "\nFirst 20 Woodall numbers: ( n × 2**n - 1)\n", @woodall[1..20]; # A003261 |
put "\nFirst 20 Woodall numbers: ( n × 2**n - 1)\n", @woodall[1..20]; # A003261 |
||
put "\nFirst 5 Cullen primes: (in terms of n)\n", @cullen.grep( &is-prime, :k )[^5]; # A005849 |
put "\nFirst 5 Cullen primes: (in terms of n)\n", @cullen.grep( &is-prime, :k )[^5]; # A005849 |
||
put "\nFirst 12 Woodall primes: (in terms of n)\n", @woodall.grep( &is-prime, :k )[^12]; # A002234</ |
put "\nFirst 12 Woodall primes: (in terms of n)\n", @woodall.grep( &is-prime, :k )[^12]; # A002234</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 20 Cullen numbers: ( n × 2**n + 1) |
<pre>First 20 Cullen numbers: ( n × 2**n + 1) |
||
Line 229: | Line 1,411: | ||
First 12 Woodall primes: (in terms of n) |
First 12 Woodall primes: (in terms of n) |
||
2 3 6 30 75 81 115 123 249 362 384 462</pre> |
2 3 6 30 75 81 115 123 249 362 384 462</pre> |
||
=={{header|Ring}}== |
|||
<syntaxhighlight lang="ring"> |
|||
load "stdlib.ring" |
|||
see "working..." + nl |
|||
see "First 20 Cullen numbers:" + nl |
|||
for n = 1 to 20 |
|||
num = n*pow(2,n)+1 |
|||
see "" + num + " " |
|||
next |
|||
see nl + nl + "First 20 Woodall numbers:" + nl |
|||
for n = 1 to 20 |
|||
num = n*pow(2,n)-1 |
|||
see "" + num + " " |
|||
next |
|||
see nl + "done..." + nl |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
working... |
|||
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 |
|||
done... |
|||
</pre> |
|||
=={{header|RPL}}== |
|||
≪ DUP R→B |
|||
1 ROT '''START''' SL '''NEXT''' |
|||
1 + B→R |
|||
≫ '<span style="color:blue">CULLIN</span>' STO |
|||
≪ <span style="color:blue">CULLIN</span> 2 - |
|||
≫ '<span style="color:blue">WDHAL</span>' STO |
|||
≪ {} 1 20 '''FOR''' n n <span style="color:blue">CULLIN</span> + '''NEXT''' ≫ EVAL |
|||
≪ {} 1 20 '''FOR''' n n <span style="color:blue">WDHAL</span> + '''NEXT''' ≫ EVAL |
|||
{{out}} |
|||
<pre> |
|||
2: { 3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 } |
|||
1: { 1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 } |
|||
</pre> |
|||
=={{header|Ruby}}== |
|||
The OpenSSL prime? methods is faster than the standard one. It still takes 1-2 minutes to calculate the first 5 Cullen primes. |
|||
<syntaxhighlight lang="ruby">require 'openssl' |
|||
cullen = Enumerator.new{|y| (1..).each{|n| y << (n*(1<<n) + 1)} } |
|||
woodall = Enumerator.new{|y| (1..).each{|n| y << (n*(1<<n) - 1)} } |
|||
cullen_primes = Enumerator.new{|y| (1..).each {|i|y << i if OpenSSL::BN.new(cullen.next).prime?}} |
|||
woodall_primes = Enumerator.new{|y| (1..).each{|i|y << i if OpenSSL::BN.new(woodall.next).prime?}} |
|||
num = 20 |
|||
puts "First #{num} Cullen numbers:\n#{cullen.first(num).join(" ")}" |
|||
puts "First #{num} Woodal numbers:\n#{woodall.first(num).join(" ")}" |
|||
puts "First 5 Cullen primes:\n#{cullen_primes.first(5).join(", ")}" |
|||
puts "First 12 Woodall primes:\n#{woodall_primes.first(12).join(", ")}" |
|||
</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 Woodal 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: |
|||
1, 141, 4713, 5795, 6611 |
|||
First 12 Woodall primes: |
|||
2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462 |
|||
</pre> |
|||
=={{header|Rust}}== |
|||
<syntaxhighlight lang="rust">// [dependencies] |
|||
// rug = "1.15.0" |
|||
use rug::integer::IsPrime; |
|||
use rug::Integer; |
|||
fn cullen_number(n: u32) -> Integer { |
|||
let num = Integer::from(n); |
|||
(num << n) + 1 |
|||
} |
|||
fn woodall_number(n: u32) -> Integer { |
|||
let num = Integer::from(n); |
|||
(num << n) - 1 |
|||
} |
|||
fn main() { |
|||
println!("First 20 Cullen numbers:"); |
|||
let cullen: Vec<String> = (1..21).map(|x| cullen_number(x).to_string()).collect(); |
|||
println!("{}", cullen.join(" ")); |
|||
println!("\nFirst 20 Woodall numbers:"); |
|||
let woodall: Vec<String> = (1..21).map(|x| woodall_number(x).to_string()).collect(); |
|||
println!("{}", woodall.join(" ")); |
|||
println!("\nFirst 5 Cullen primes in terms of n:"); |
|||
let cullen_primes: Vec<String> = (1..) |
|||
.filter_map(|x| match cullen_number(x).is_probably_prime(25) { |
|||
IsPrime::No => None, |
|||
_ => Some(x.to_string()), |
|||
}) |
|||
.take(5) |
|||
.collect(); |
|||
println!("{}", cullen_primes.join(" ")); |
|||
println!("\nFirst 12 Woodall primes in terms of n:"); |
|||
let woodall_primes: Vec<String> = (1..) |
|||
.filter_map(|x| match woodall_number(x).is_probably_prime(25) { |
|||
IsPrime::No => None, |
|||
_ => Some(x.to_string()), |
|||
}) |
|||
.take(12) |
|||
.collect(); |
|||
println!("{}", woodall_primes.join(" ")); |
|||
}</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|Scala}}== |
|||
{{trans|Java}} |
|||
<syntaxhighlight lang="Scala"> |
|||
import java.math.BigInteger |
|||
import scala.annotation.tailrec |
|||
object CullenAndWoodhall extends App { |
|||
val Certainty = 20 |
|||
var number: BigInteger = _ |
|||
var power: BigInteger = _ |
|||
var count: Int = _ |
|||
var primeIndex: Int = _ |
|||
sealed trait NumberType |
|||
case object Cullen extends NumberType |
|||
case object Woodhall extends NumberType |
|||
numberSequence(20, Cullen) |
|||
numberSequence(20, Woodhall) |
|||
primeSequence(5, Cullen) |
|||
primeSequence(12, Woodhall) |
|||
def numberSequence(aCount: Int, aNumberType: NumberType): Unit = { |
|||
println(s"\nThe first $aCount $aNumberType numbers are:") |
|||
numberInitialise() |
|||
(1 to aCount).foreach { _ => |
|||
print(nextNumber(aNumberType).toString + " ") |
|||
} |
|||
println() |
|||
} |
|||
def primeSequence(aCount: Int, aNumberType: NumberType): Unit = { |
|||
println(s"\nThe indexes of the first $aCount $aNumberType primes are:") |
|||
primeInitialise() |
|||
@tailrec |
|||
def findPrimes(): Unit = { |
|||
if (count < aCount) { |
|||
if (nextNumber(aNumberType).isProbablePrime(Certainty)) { |
|||
print(primeIndex + " ") |
|||
count += 1 |
|||
} |
|||
primeIndex += 1 |
|||
findPrimes() |
|||
} |
|||
} |
|||
findPrimes() |
|||
println() |
|||
} |
|||
def nextNumber(aNumberType: NumberType): BigInteger = { |
|||
number = number.add(BigInteger.ONE) |
|||
power = power.shiftLeft(1) |
|||
aNumberType match { |
|||
case Cullen => number.multiply(power).add(BigInteger.ONE) |
|||
case Woodhall => number.multiply(power).subtract(BigInteger.ONE) |
|||
} |
|||
} |
|||
def numberInitialise(): Unit = { |
|||
number = BigInteger.ZERO |
|||
power = BigInteger.ONE |
|||
} |
|||
def primeInitialise(): Unit = { |
|||
count = 0 |
|||
primeIndex = 1 |
|||
numberInitialise() |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 20 Cullen numbers are: |
|||
3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 |
|||
The first 20 Woodhall numbers are: |
|||
1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 |
|||
The indexes of the first 5 Cullen primes are: |
|||
1 141 4713 5794 6611 |
|||
The indexes of the first 12 Woodhall primes are: |
|||
2 3 6 30 75 81 115 123 249 362 384 462 |
|||
</pre> |
|||
=={{header|Sidef}}== |
|||
<syntaxhighlight lang="ruby">func cullen(n) { n * (1 << n) + 1 } |
|||
func woodall(n) { n * (1 << n) - 1 } |
|||
say "First 20 Cullen numbers:" |
|||
say cullen.map(1..20).join(' ') |
|||
say "\nFirst 20 Woodall numbers:" |
|||
say woodall.map(1..20).join(' ') |
|||
say "\nFirst 5 Cullen primes: (in terms of n)" |
|||
say 5.by { cullen(_).is_prime }.join(' ') |
|||
say "\nFirst 12 Woodall primes: (in terms of n)" |
|||
say 12.by { woodall(_).is_prime }.join(' ')</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|Verilog}}== |
|||
<syntaxhighlight lang="verilog">module main; |
|||
integer n, num; |
|||
initial begin |
|||
$display("First 20 Cullen numbers:"); |
|||
for(n = 1; n <= 20; n=n+1) |
|||
begin |
|||
num = n * (2 ** n) + 1; |
|||
$write(num, " "); |
|||
end |
|||
$display(""); |
|||
$display("First 20 Woodall numbers:"); |
|||
for(n = 1; n <= 20; n=n+1) |
|||
begin |
|||
num = n * (2 ** n) - 1; |
|||
$write(num, " "); |
|||
end |
|||
$finish ; |
|||
end |
|||
endmodule</syntaxhighlight> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 234: | Line 1,697: | ||
{{libheader|Wren-big}} |
{{libheader|Wren-big}} |
||
Cullen primes limited to first 2 as very slow after that. |
Cullen primes limited to first 2 as very slow after that. |
||
< |
<syntaxhighlight lang="wren">import "./big" for BigInt |
||
var cullen = Fn.new { |n| (BigInt.one << n) * n + 1 } |
var cullen = Fn.new { |n| (BigInt.one << n) * n + 1 } |
||
Line 269: | Line 1,732: | ||
n = n + 1 |
n = n + 1 |
||
} |
} |
||
System.print()</ |
System.print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 289: | Line 1,752: | ||
{{libheader|Wren-gmp}} |
{{libheader|Wren-gmp}} |
||
Cullen primes still slow to emerge, just over 10 seconds overall. |
Cullen primes still slow to emerge, just over 10 seconds overall. |
||
< |
<syntaxhighlight lang="wren">/* Cullen_and_woodall_numbers_2.wren */ |
||
import "./gmp" for Mpz |
import "./gmp" for Mpz |
||
Line 326: | Line 1,789: | ||
n = n + 1 |
n = n + 1 |
||
} |
} |
||
System.print()</ |
System.print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 341: | Line 1,804: | ||
First 12 Woodall primes (in terms of n): |
First 12 Woodall primes (in terms of n): |
||
2 3 6 30 75 81 115 123 249 362 384 462 |
2 3 6 30 75 81 115 123 249 362 384 462 |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
{{trans|11l}} |
|||
<syntaxhighlight lang "XPL0">func Cullen(N); |
|||
int N; |
|||
return N<<N + 1; |
|||
func Woodall(N); |
|||
int N; |
|||
return N<<N - 1; |
|||
int I; |
|||
[Text(0, "First 20 Cullen numbers:^m^j"); |
|||
for I:= 1 to 20 do |
|||
[IntOut(0, Cullen(I)); ChOut(0, ^ )]; |
|||
CrLf(0); |
|||
CrLf(0); |
|||
Text(0, "First 20 Woodall numbers:^m^j"); |
|||
for I:= 1 to 20 do |
|||
[IntOut(0, Woodall(I)); ChOut(0, ^ )]; |
|||
CrLf(0); |
|||
]</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 |
|||
</pre> |
</pre> |
Latest revision as of 07:04, 1 February 2024
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
A Cullen number is a number of the form n × 2n + 1 where n is a natural number.
A Woodall number is very similar. It is a number of the form n × 2n - 1 where n is a natural number.
So for each n the associated Cullen number and Woodall number differ by 2.
Woodall numbers are sometimes referred to as Riesel numbers or Cullen numbers of the second kind.
Cullen primes are Cullen numbers that are prime. Similarly, Woodall primes are Woodall numbers that are prime.
It is common to list the Cullen and Woodall primes by the value of n rather than the full evaluated expression. They tend to get very large very quickly. For example, the third Cullen prime, n == 4713, has 1423 digits when evaluated.
- Task
- Write procedures to find Cullen numbers and Woodall numbers.
- Use those procedures to find and show here, on this page the first 20 of each.
- Stretch
- Find and show the first 5 Cullen primes in terms of n.
- Find and show the first 12 Woodall primes in terms of n.
- See also
11l
F cullen(n)
R (n << n) + 1
F woodall(n)
R (n << n) - 1
print(‘First 20 Cullen numbers:’)
L(i) 1..20
print(cullen(i), end' ‘ ’)
print()
print()
print(‘First 20 Woodall numbers:’)
L(i) 1..20
print(woodall(i), end' ‘ ’)
print()
- Output:
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
ALGOL 68
Uses Algol 68Gs LONG LONG INT for long integers. The number of digits must be specified and appears to affect the run time as larger sies are specified. This sample only shows the first two Cullen primes as the time taken to find the third is rather long.
BEGIN # find Cullen and Woodall numbers and determine which are prime #
# a Cullen number n is n2^2 + 1, Woodall number is n2^n - 1 #
PR read "primes.incl.a68" PR # include prime utilities #
PR precision 800 PR # set number of digits for Algol 68G LONG LONG INT #
# returns the nth Cullen number #
OP CULLEN = ( INT n )LONG LONG INT: n * LONG LONG INT(2)^n + 1;
# returns the nth Woodall number #
OP WOODALL = ( INT n )LONG LONG INT: CULLEN n - 2;
# show the first 20 Cullen numbers #
print( ( "1st 20 Cullen numbers:" ) );
FOR n TO 20 DO
print( ( " ", whole( CULLEN n, 0 ) ) )
OD;
print( ( newline ) );
# show the first 20 Woodall numbers #
print( ( "1st 20 Woodall numbers:" ) );
FOR n TO 20 DO
print( ( " ", whole( WOODALL n, 0 ) ) )
OD;
print( ( newline ) );
BEGIN # first 2 Cullen primes #
print( ( "Index of the 1st 2 Cullen primes:" ) );
LONG LONG INT power of 2 := 1;
INT prime count := 0;
FOR n WHILE prime count < 2 DO
power of 2 *:= 2;
LONG LONG INT c n = ( n * power of 2 ) + 1;
IF is probably prime( c n ) THEN
prime count +:= 1;
print( ( " ", whole( n, 0 ) ) )
FI
OD;
print( ( newline ) )
END;
BEGIN # first 12 Woodall primes #
print( ( "Index of the 1st 12 Woodall primes:" ) );
LONG LONG INT power of 2 := 1;
INT prime count := 0;
FOR n WHILE prime count < 12 DO
power of 2 *:= 2;
LONG LONG INT w n = ( n * power of 2 ) - 1;
IF is probably prime( w n ) THEN
prime count +:= 1;
print( ( " ", whole( n, 0 ) ) )
FI
OD;
print( ( newline ) )
END
END
- Output:
1st 20 Cullen numbers: 3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 1st 20 Woodall numbers: 1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 Index of the 1st 2 Cullen primes: 1 141 Index of the 1st 12 Woodall primes: 2 3 6 30 75 81 115 123 249 362 384 462
Arturo
cullen: function [n]->
inc n * 2^n
woodall: function [n]->
dec n * 2^n
print ["First 20 cullen numbers:" join.with:" " to [:string] map 1..20 => cullen]
print ["First 20 woodall numbers:" join.with:" " to [:string] map 1..20 => woodall]
- Output:
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
Asymptote
int num = 0;
write("First 20 Cullen numbers:");
for(int n = 1; n < 20; ++n) {
num = n * (2^n) + 1;
write(" ", num, suffix=none);
}
write("");
write("First 20 Woodall numbers:");
for(int n = 1; n < 20; ++n) {
num = n * (2^n) - 1;
write(" ", num, suffix=none);
}
AWK
# syntax: GAWK -f CULLEN_AND_WOODALL_NUMBERS.AWK
BEGIN {
start = 1
stop = 20
printf("Cullen %d-%d:",start,stop)
for (n=start; n<=stop; n++) {
printf(" %d",n*(2^n)+1)
}
printf("\n")
printf("Woodall %d-%d:",start,stop)
for (n=start; n<=stop; n++) {
printf(" %d",n*(2^n)-1)
}
printf("\n")
exit(0)
}
- Output:
Cullen 1-20: 3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 Woodall 1-20: 1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519
BASIC
Applesoft BASIC
10 REM Cullen and Woodall numbers
20 HOME : REM 20 CLS for Chipmunk Basic
30 PRINT "First 20 Cullen numbers:"
40 FOR n = 1 TO 20
50 num = n*(2^n)+1
60 PRINT INT(num);" ";
70 NEXT
80 PRINT : PRINT
90 PRINT "First 20 Woodall numbers:"
100 FOR n = 1 TO 20
110 num = n*(2^n)-1
120 PRINT INT(num);" ";
130 NEXT n
140 END
BASIC256
print "First 20 Cullen numbers:"
for n = 1 to 20
num = n * (2^n)+1
print int(num); " ";
next
print : print
print "First 20 Woodall numbers:"
for n = 1 to 20
num = n * (2^n)-1
print int(num); " ";
next n
end
- Output:
Igual que la entrada de FreeBASIC.
Chipmunk Basic
10 REM Cullen and Woodall numbers
20 CLS : REM 20 HOME for Applesoft BASIC
30 PRINT "First 20 Cullen numbers:"
40 FOR n = 1 TO 20
50 num = n*(2^n)+1
60 PRINT INT(num);" ";
70 NEXT
80 PRINT : PRINT
90 PRINT "First 20 Woodall numbers:"
100 FOR n = 1 TO 20
110 num = n*(2^n)-1
120 PRINT INT(num);" ";
130 NEXT n
140 END
- Output:
Same as FreeBASIC entry.
FreeBASIC
Dim As Uinteger n, num
Print "First 20 Cullen numbers:"
For n = 1 To 20
num = n * (2^n)+1
Print num; " ";
Next
Print !"\n\nFirst 20 Woodall numbers:"
For n = 1 To 20
num = n * (2^n)-1
Print num; " ";
Next n
Sleep
- Output:
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
Gambas
Public Sub Main()
Dim n, num As Integer
Print "First 20 Cullen numbers:"
For n = 1 To 20
num = n * (2 ^ n) + 1
Print num; " ";
Next
Print "\n\nFirst 20 Woodall numbers:"
For n = 1 To 20
num = n * (2 ^ n) - 1
Print num; " ";
Next
End
- Output:
Same as FreeBASIC entry.
GW-BASIC
The Chipmunk Basic solution works without any changes.
PureBasic
OpenConsole()
PrintN("First 20 Cullen numbers:")
For n.i = 1 To 20
num = n * Pow(2, n)+1
Print(Str(num) + " ")
Next
PrintN(#CRLF$ + "First 20 Woodall numbers:")
For n.i = 1 To 20
num = n * Pow(2, n)-1
Print(Str(num) + " ")
Next n
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()
- Output:
Igual que la entrada de FreeBASIC.
QBasic
DIM num AS LONG ''comment this line for True BASIC
PRINT "First 20 Cullen numbers:"
FOR n = 1 TO 20
LET num = n * (2 ^ n) + 1
PRINT num;
NEXT n
PRINT
PRINT
PRINT "First 20 Woodall numbers:"
FOR n = 1 TO 20
LET num = n * (2 ^ n) - 1
PRINT num;
NEXT n
END
- Output:
Igual que la entrada de FreeBASIC.
Run BASIC
print "First 20 Cullen numbers:"
for n = 1 to 20
num = n * (2^n)+1
print int(num); " ";
next
print : print
print "First 20 Woodall numbers:"
for n = 1 to 20
num = n * (2^n)-1
print int(num); " ";
next n
end
- Output:
Same as FreeBASIC entry.
Tiny BASIC
REM Rosetta Code problem: https://rosettacode.org/wiki/Cullen_and_Woodall_numbers
REM by Jjuanhdez, 03/2023
REM TinyBasic does not support values greater than 32767
PRINT "First 11 Cullen numbers:"
LET N = 0
LET I = 1
10 IF I = 12 THEN GOTO 20
GOSUB 50
LET N = (I*R) +1
PRINT N, " "
LET I = I+1
GOTO 10
20 PRINT ""
PRINT "First 11 Woodall numbers:"
LET I = 1
30 IF I = 12 THEN GOTO 40
GOSUB 50
LET N = (I*R) -1
PRINT N, " "
LET I = I+1
GOTO 30
40 END
50 REM Exponent calculation
LET A = 2
LET B = I
LET X = 1
LET R = 2
60 IF X >= B THEN RETURN
LET T = R
IF R < A THEN LET R = A*A
IF T < A THEN GOTO 70
IF R >= A THEN LET R = R*A
70 LET X = X+1
GOTO 60
- Output:
First 11 Cullen numbers: 3 9 25 65 161 385 897 2049 4609 10241 22529 First 11 Woodall numbers: 1 7 23 63 159 383 895 2047 4607 10239 22527
True BASIC
REM DIM num AS LONG !uncomment this line for QBasic
PRINT "First 20 Cullen numbers:"
FOR n = 1 TO 20
LET num = n * (2 ^ n) + 1
PRINT num;
NEXT n
PRINT
PRINT
PRINT "First 20 Woodall numbers:"
FOR n = 1 TO 20
LET num = n * (2 ^ n) - 1
PRINT num;
NEXT n
END
- Output:
Igual que la entrada de FreeBASIC.
MSX Basic
The Chipmunk Basic solution works without any changes.
XBasic
PROGRAM "progname"
VERSION "0.0000"
IMPORT "xma"
DECLARE FUNCTION Entry ()
FUNCTION Entry ()
PRINT "First 20 Cullen numbers:"
FOR n = 1 TO 20
num! = n * POWER (2, n) + 1
PRINT num!;
NEXT n
PRINT
PRINT
PRINT "First 20 Woodall numbers:"
FOR n = 1 TO 20
num! = n * POWER (2, n) - 1
PRINT num!;
NEXT n
END FUNCTION
END PROGRAM
Yabasic
print "First 20 Cullen numbers:"
for n = 1 to 20
num = n * (2^n)+1
print num, " ";
next
print "\n\nFirst 20 Woodall numbers:"
for n = 1 to 20
num = n * (2^n)-1
print num, " ";
next n
print
end
- Output:
Igual que la entrada de FreeBASIC.
C++
#include <cstdint>
#include <iostream>
#include <string>
uint32_t number, power;
void number_initialise() {
number = 0;
power = 1;
}
enum NumberType { Cullen, Woodhall };
uint32_t next_number(const NumberType& number_type) {
number += 1;
power <<= 1;
switch ( number_type ) {
case Cullen: return number * power + 1;
case Woodhall: return number * power - 1;
};
return 0;
}
void number_sequence(const uint32_t& count, const NumberType& number_type) {
std::string type = ( number_type == Cullen ) ? "Cullen" : "Woodhall";
std::cout << "The first " << count << " " << type << " numbers are:" << std::endl;
number_initialise();
for ( uint32_t index = 1; index <= count; ++index ) {
std::cout << next_number(number_type) << " ";
}
std::cout << std::endl << std::endl;
}
int main() {
number_sequence(20, Cullen);
number_sequence(20, Woodhall);
}
- Output:
The first 20 Cullen numbers are: 3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 The first 20 Woodhall numbers are: 1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519
Delphi
uses SysUtils,StdCtrls;
procedure CullenWoodallTest(Memo: TMemo);
implementation
procedure FindCullenNumbers(Memo: TMemo);
var N,R: integer;
var S: string;
begin
S:='';
Memo.Lines.Add('First 20 Cullen Numbers:');
for N:=1 to 20 do
begin
R:=N * (1 shl N) + 1;
S:=S+IntToStr(R)+' ';
end;
Memo.Lines.Add(S);
end;
procedure FindWoodallNumbers(Memo: TMemo);
var N,R: integer;
var S: string;
begin
S:='';
Memo.Lines.Add('First 20 Woodall Numbers:');
for N:=1 to 20 do
begin
R:=N * (1 shl N) - 1;
S:=S+IntToStr(R)+' ';
end;
Memo.Lines.Add(S);
end;
procedure CullenWoodallTest(Memo: TMemo);
begin
FindCullenNumbers(Memo);
FindWoodallNumbers(Memo);
end;
- Output:
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
EasyLang
for n = 1 to 20
write n * pow 2 n + 1 & " "
.
print ""
for n = 1 to 20
write n * pow 2 n - 1 & " "
.
print ""
F#
// Cullen and Woodall numbers. Nigel Galloway: January 14th., 2022
let Cullen,Woodall=let fG n (g:int)=(bigint g)*2I**g+n in fG 1I, fG -1I
Seq.initInfinite((+)1>>Cullen)|>Seq.take 20|>Seq.iter(printf "%A "); printfn ""
Seq.initInfinite((+)1>>Woodall)|>Seq.take 20|>Seq.iter(printf "%A "); printfn ""
Seq.initInfinite((+)1)|>Seq.filter(fun n->let mutable n=Woodall n in Open.Numeric.Primes.MillerRabin.IsProbablePrime &n)|>Seq.take 12|>Seq.iter(printf "%A "); printfn ""
Seq.initInfinite((+)1)|>Seq.filter(fun n->let mutable n=Cullen n in Open.Numeric.Primes.MillerRabin.IsProbablePrime &n)|>Seq.take 5|>Seq.iter(printf "%A "); printfn ""
- Output:
3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 2 3 6 30 75 81 115 123 249 362 384 462 1 141 4713 5795 6611
Factor
USING: arrays kernel math math.vectors prettyprint ranges
sequences ;
20 [1..b] [ dup 2^ * 1 + ] map dup 2 v-n 2array simple-table.
- Output:
3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519
FutureBasic
local fn CullenAndWoodall( limit as long )
NSUInteger i, cullen, woodall
printf @"%13s %9s", fn StringUTF8String( @"Cullen" ), fn StringUTF8String( @"Woodall" )
for i = 1 to limit
cullen = i * ( 2^i ) + 1
woodall = i * ( 2^i ) - 1
printf @"%3lu %9lu %9lu", i, cullen, woodall
next
end fn
fn CullenAndWoodall( 20 )
HandleEvents
- Output:
Cullen Woodall 1 3 1 2 9 7 3 25 23 4 65 63 5 161 159 6 385 383 7 897 895 8 2049 2047 9 4609 4607 10 10241 10239 11 22529 22527 12 49153 49151 13 106497 106495 14 229377 229375 15 491521 491519 16 1048577 1048575 17 2228225 2228223 18 4718593 4718591 19 9961473 9961471 20 20971521 20971519
Go
package main
import (
"fmt"
big "github.com/ncw/gmp"
)
func cullen(n uint) *big.Int {
one := big.NewInt(1)
bn := big.NewInt(int64(n))
res := new(big.Int).Lsh(one, n)
res.Mul(res, bn)
return res.Add(res, one)
}
func woodall(n uint) *big.Int {
res := cullen(n)
return res.Sub(res, big.NewInt(2))
}
func main() {
fmt.Println("First 20 Cullen numbers (n * 2^n + 1):")
for n := uint(1); n <= 20; n++ {
fmt.Printf("%d ", cullen(n))
}
fmt.Println("\n\nFirst 20 Woodall numbers (n * 2^n - 1):")
for n := uint(1); n <= 20; n++ {
fmt.Printf("%d ", woodall(n))
}
fmt.Println("\n\nFirst 5 Cullen primes (in terms of n):")
count := 0
for n := uint(1); count < 5; n++ {
cn := cullen(n)
if cn.ProbablyPrime(15) {
fmt.Printf("%d ", n)
count++
}
}
fmt.Println("\n\nFirst 12 Woodall primes (in terms of n):")
count = 0
for n := uint(1); count < 12; n++ {
cn := woodall(n)
if cn.ProbablyPrime(15) {
fmt.Printf("%d ", n)
count++
}
}
fmt.Println()
}
- Output:
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 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
Haskell
findCullen :: Int -> Integer
findCullen n = toInteger ( n * 2 ^ n + 1 )
cullens :: [Integer]
cullens = map findCullen [1 .. 20]
woodalls :: [Integer]
woodalls = map (\i -> i - 2 ) cullens
main :: IO ( )
main = do
putStrLn "First 20 Cullen numbers:"
print cullens
putStrLn "First 20 Woodall numbers:"
print woodalls
- Output:
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]
J
cullen=: {{ y* 1+2x^y }}
woodall=: {{ y*_1+2x^y }}
Task example:
cullen 1+i.20
3 10 27 68 165 390 903 2056 4617 10250 22539 49164 106509 229390 491535 1048592 2228241 4718610 9961491 20971540
woodall 1+i.20
1 6 21 60 155 378 889 2040 4599 10230 22517 49140 106483 229362 491505 1048560 2228207 4718574 9961453 20971500
Java
import java.math.BigInteger;
public final class CullenAndWoodhall {
public static void main(String[] aArgs) {
numberSequence(20, NumberType.Cullen);
numberSequence(20, NumberType.Woodhall);
primeSequence(5, NumberType.Cullen);
primeSequence(12, NumberType.Woodhall);
}
private enum NumberType { Cullen, Woodhall }
private static void numberSequence(int aCount, NumberType aNumberType) {
System.out.println();
System.out.println("The first " + aCount + " " + aNumberType + " numbers are:");
numberInitialise();
for ( int index = 1; index <= aCount; index++ ) {
System.out.print(nextNumber(aNumberType) + " ");
}
System.out.println();
}
private static void primeSequence(int aCount, NumberType aNumberType) {
System.out.println();
System.out.println("The indexes of the first " + aCount + " " + aNumberType + " primes are:");
primeInitialise();
while ( count < aCount ) {
if ( nextNumber(aNumberType).isProbablePrime(CERTAINTY) ) {
System.out.print(primeIndex + " ");
count += 1;
}
primeIndex += 1;
}
System.out.println();
}
private static BigInteger nextNumber(NumberType aNumberType) {
number = number.add(BigInteger.ONE);
power = power.shiftLeft(1);
return switch ( aNumberType ) {
case Cullen -> number.multiply(power).add(BigInteger.ONE);
case Woodhall -> number.multiply(power).subtract(BigInteger.ONE);
};
}
private static void numberInitialise() {
number = BigInteger.ZERO;
power = BigInteger.ONE;
}
private static void primeInitialise() {
count = 0;
primeIndex = 1;
numberInitialise();
}
private static BigInteger number;
private static BigInteger power;
private static int count;
private static int primeIndex;
private static final int CERTAINTY = 20;
}
- Output:
The first 20 Cullen numbers are: 3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 The first 20 Woodhall numbers are: 1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 The indexes of the first 5 Cullen primes are: 1 141 4713 5794 6611 The indexes of the first 12 Woodhall primes are: 2 3 6 30 75 81 115 123 249 362 384 462
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.
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
- Output:
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
Julia
using Lazy
using Primes
cullen(n, two = BigInt(2)) = n * two^n + 1
woodall(n, two = BigInt(2)) = n * two^n - 1
primecullens = @>> Lazy.range() filter(n -> isprime(cullen(n)))
primewoodalls = @>> Lazy.range() filter(n -> isprime(woodall(n)))
println("First 20 Cullen numbers: ( n × 2**n + 1)\n", [cullen(n, 2) for n in 1:20]) # A002064
println("First 20 Woodall numbers: ( n × 2**n - 1)\n", [woodall(n, 2) for n in 1:20]) # A003261
println("\nFirst 5 Cullen primes: (in terms of n)\n", take(5, primecullens)) # A005849
println("\nFirst 12 Woodall primes: (in terms of n)\n", Int.(collect(take(12, primewoodalls)))) # A002234
- Output:
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 5 Cullen primes: (in terms of n) List: (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]
Lua
function T(t) return setmetatable(t, {__index=table}) end
table.range = function(t,n) local s=T{} for i=1,n do s[i]=i end return s end
table.map = function(t,f) local s=T{} for i=1,#t do s[i]=f(t[i]) end return s end
function cullen(n) return (n<<n)+1 end
print("First 20 Cullen numbers:")
print(T{}:range(20):map(cullen):concat(" "))
function woodall(n) return (n<<n)-1 end
print("First 20 Woodall numbers:")
print(T{}:range(20):map(woodall):concat(" "))
- Output:
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
Mathematica /Wolfram Language
ClearAll[CullenNumber, WoodallNumber]
SetAttributes[{CullenNumber, WoodallNumber}, Listable]
CullenNumber[n_Integer] := n 2^n + 1
WoodallNumber[n_Integer] := n 2^n - 1
CullenNumber[Range[20]]
WoodallNumber[Range[20]]
cps = {};
Do[
If[PrimeQ[CullenNumber[i]],
AppendTo[cps, i];
If[Length[cps] >= 5, Break[]]
]
,
{i, 1, \[Infinity]}
]
cps
wps = {};
Do[
If[PrimeQ[WoodallNumber[i]],
AppendTo[wps, i];
If[Length[wps] >= 12, Break[]]
]
,
{i, 1, \[Infinity]}
];
wps
- Output:
{3, 9, 25, 65, 161, 385, 897, 2049, 4609, 10241, 22529, 49153, 106497, 229377, 491521, 1048577, 2228225, 4718593, 9961473, 20971521} {1, 7, 23, 63, 159, 383, 895, 2047, 4607, 10239, 22527, 49151, 106495, 229375, 491519, 1048575, 2228223, 4718591, 9961471, 20971519} {1, 141, 4713, 5795, 6611} {2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462}
Maxima
/* Functions */
cullen(n):=(n*2^n)+1$
woodall(n):=(n*2^n)-1$
/* Test cases */
makelist(cullen(i),i,20);
makelist(woodall(i),i,20);
- Output:
[3,9,25,65,161,385,897,2049,4609,10241,22529,49153,106497,229377,491521,1048577,2228225,4718593,9961473,20971521] [1,7,23,63,159,383,895,2047,4607,10239,22527,49151,106495,229375,491519,1048575,2228223,4718591,9961471,20971519]
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()
- Output:
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
PARI/GP
/* Define the Cullen and Woodall number functions */
cullen(n) = n * 2^n + 1;
woodall(n) = n * 2^n - 1;
{
/* Generate the first 20 Cullen and Woodall numbers */
print(vector(20, n, cullen(n)));
print(vector(20, n, woodall(n)));
/* Find the first 5 Cullen prime numbers */
cps = [];
for(i = 1, +oo,
if(isprime(cullen(i)),
cps = concat(cps, i);
if(#cps >= 5, break);
);
);
print(cps);
/* Find the first 12 Woodall prime numbers */
wps = [];
for(i = 1, +oo,
if(isprime(woodall(i)),
wps = concat(wps, i);
if(#wps >= 12, break);
);
);
print(wps);
}
- Output:
[3, 9, 25, 65, 161, 385, 897, 2049, 4609, 10241, 22529, 49153, 106497, 229377, 491521, 1048577, 2228225, 4718593, 9961473, 20971521] [1, 7, 23, 63, 159, 383, 895, 2047, 4607, 10239, 22527, 49151, 106495, 229375, 491519, 1048575, 2228223, 4718591, 9961471, 20971519] [1, 141, 4713, 5795, 6611] [2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462]
Perl
use strict;
use warnings;
use bigint;
use ntheory 'is_prime';
use constant Inf => 1e10;
sub cullen {
my($n,$c) = @_;
($n * 2**$n) + $c;
}
my($m,$n);
($m,$n) = (20,0);
print "First $m Cullen numbers:\n";
print do { $n < $m ? (++$n and cullen($_,1) . ' ') : last } for 1 .. Inf;
($m,$n) = (20,0);
print "\n\nFirst $m Woodall numbers:\n";
print do { $n < $m ? (++$n and cullen($_,-1) . ' ') : last } for 1 .. Inf;
($m,$n) = (5,0);
print "\n\nFirst $m Cullen primes: (in terms of n)\n";
print do { $n < $m ? (!!is_prime(cullen $_,1) and ++$n and "$_ ") : last } for 1 .. Inf;
($m,$n) = (12,0);
print "\n\nFirst $m Woodall primes: (in terms of n)\n";
print do { $n < $m ? (!!is_prime(cullen $_,-1) and ++$n and "$_ ") : last } for 1 .. Inf;
- Output:
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
Phix
with javascript_semantics atom t0 = time() include mpfr.e procedure cullen(mpz r, integer n) mpz_ui_pow_ui(r,2,n) mpz_mul_si(r,r,n) mpz_add_si(r,r,1) end procedure procedure woodall(mpz r, integer n) cullen(r,n) mpz_sub_si(r,r,2) end procedure sequence c = {}, w = {} mpz z = mpz_init() for i=1 to 20 do cullen(z,i) c = append(c,mpz_get_str(z)) mpz_sub_si(z,z,2) w = append(w,mpz_get_str(z)) end for printf(1," Cullen[1..20]:%s\nWoodall[1..20]:%s\n",{join(c),join(w)}) atom t1 = time()+1 c = {} integer n = 1 while length(c)<iff(platform()=JS?2:5) do cullen(z,n) if mpz_prime(z) then c = append(c,sprint(n)) end if n += 1 if time()>t1 and platform()!=JS then progress("c(%d) [needs to get to 6611], %d found\r",{n,length(c)}) t1 = time()+2 end if end while if platform()!=JS then progress("") end if printf(1,"First 5 Cullen primes (in terms of n):%s\n",{join(c)}) w = {} n = 1 while length(w)<12 do woodall(z,n) if mpz_prime(z) then w = append(w,sprint(n)) end if n += 1 end while printf(1,"First 12 Woodall primes (in terms of n):%s\n",{join(w)}) ?elapsed(time()-t0)
- Output:
Cullen[1..20]:3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 Woodall[1..20]: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 "34.4s"
Note the time given is for desktop/Phix 64bit, for comparison the Julia entry took about 20s on the same box. On 32-bit it is nearly 5 times slower (2 minutes and 38s) and hence under pwa/p2js in a browser (which is inherently 32bit) it is limited to the first 2 cullen primes only, but manages that in 0.4s.
Python
print("working...")
print("First 20 Cullen numbers:")
for n in range(1,21):
num = n*pow(2,n)+1
print(str(num),end= " ")
print()
print("First 20 Woodall numbers:")
for n in range(1,21):
num = n*pow(2,n)-1
print(str(num),end=" ")
print()
print("done...")
- Output:
working... 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 done...
Bit Shift
def cullen(n): return((n<<n)+1)
def woodall(n): return((n<<n)-1)
print("First 20 Cullen numbers:")
for i in range(1,21):
print(cullen(i),end=" ")
print()
print()
print("First 20 Woodall numbers:")
for i in range(1,21):
print(woodall(i),end=" ")
print()
- Output:
Same as Quackery.
Quackery
[ dup << 1+ ] is cullen ( n --> n )
[ dup << 1 - ] is woodall ( n --> n )
say "First 20 Cullen numbers:" cr
20 times [ i^ 1+ cullen echo sp ] cr
cr
say "First 20 Woodall numbers:" cr
20 times [ i^ 1+ woodall echo sp ] cr
- Output:
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
Raku
my @cullen = ^∞ .map: { $_ × 1 +< $_ + 1 };
my @woodall = ^∞ .map: { $_ × 1 +< $_ - 1 };
put "First 20 Cullen numbers: ( n × 2**n + 1)\n", @cullen[1..20]; # A002064
put "\nFirst 20 Woodall numbers: ( n × 2**n - 1)\n", @woodall[1..20]; # A003261
put "\nFirst 5 Cullen primes: (in terms of n)\n", @cullen.grep( &is-prime, :k )[^5]; # A005849
put "\nFirst 12 Woodall primes: (in terms of n)\n", @woodall.grep( &is-prime, :k )[^12]; # A002234
- Output:
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 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
Ring
load "stdlib.ring"
see "working..." + nl
see "First 20 Cullen numbers:" + nl
for n = 1 to 20
num = n*pow(2,n)+1
see "" + num + " "
next
see nl + nl + "First 20 Woodall numbers:" + nl
for n = 1 to 20
num = n*pow(2,n)-1
see "" + num + " "
next
see nl + "done..." + nl
- Output:
working... 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 done...
RPL
≪ DUP R→B 1 ROT START SL NEXT 1 + B→R ≫ 'CULLIN' STO ≪ CULLIN 2 - ≫ 'WDHAL' STO
≪ {} 1 20 FOR n n CULLIN + NEXT ≫ EVAL ≪ {} 1 20 FOR n n WDHAL + NEXT ≫ EVAL
- Output:
2: { 3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 } 1: { 1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 }
Ruby
The OpenSSL prime? methods is faster than the standard one. It still takes 1-2 minutes to calculate the first 5 Cullen primes.
require 'openssl'
cullen = Enumerator.new{|y| (1..).each{|n| y << (n*(1<<n) + 1)} }
woodall = Enumerator.new{|y| (1..).each{|n| y << (n*(1<<n) - 1)} }
cullen_primes = Enumerator.new{|y| (1..).each {|i|y << i if OpenSSL::BN.new(cullen.next).prime?}}
woodall_primes = Enumerator.new{|y| (1..).each{|i|y << i if OpenSSL::BN.new(woodall.next).prime?}}
num = 20
puts "First #{num} Cullen numbers:\n#{cullen.first(num).join(" ")}"
puts "First #{num} Woodal numbers:\n#{woodall.first(num).join(" ")}"
puts "First 5 Cullen primes:\n#{cullen_primes.first(5).join(", ")}"
puts "First 12 Woodall primes:\n#{woodall_primes.first(12).join(", ")}"
- Output:
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 Woodal 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: 1, 141, 4713, 5795, 6611 First 12 Woodall primes: 2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462
Rust
// [dependencies]
// rug = "1.15.0"
use rug::integer::IsPrime;
use rug::Integer;
fn cullen_number(n: u32) -> Integer {
let num = Integer::from(n);
(num << n) + 1
}
fn woodall_number(n: u32) -> Integer {
let num = Integer::from(n);
(num << n) - 1
}
fn main() {
println!("First 20 Cullen numbers:");
let cullen: Vec<String> = (1..21).map(|x| cullen_number(x).to_string()).collect();
println!("{}", cullen.join(" "));
println!("\nFirst 20 Woodall numbers:");
let woodall: Vec<String> = (1..21).map(|x| woodall_number(x).to_string()).collect();
println!("{}", woodall.join(" "));
println!("\nFirst 5 Cullen primes in terms of n:");
let cullen_primes: Vec<String> = (1..)
.filter_map(|x| match cullen_number(x).is_probably_prime(25) {
IsPrime::No => None,
_ => Some(x.to_string()),
})
.take(5)
.collect();
println!("{}", cullen_primes.join(" "));
println!("\nFirst 12 Woodall primes in terms of n:");
let woodall_primes: Vec<String> = (1..)
.filter_map(|x| match woodall_number(x).is_probably_prime(25) {
IsPrime::No => None,
_ => Some(x.to_string()),
})
.take(12)
.collect();
println!("{}", woodall_primes.join(" "));
}
- Output:
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
Scala
import java.math.BigInteger
import scala.annotation.tailrec
object CullenAndWoodhall extends App {
val Certainty = 20
var number: BigInteger = _
var power: BigInteger = _
var count: Int = _
var primeIndex: Int = _
sealed trait NumberType
case object Cullen extends NumberType
case object Woodhall extends NumberType
numberSequence(20, Cullen)
numberSequence(20, Woodhall)
primeSequence(5, Cullen)
primeSequence(12, Woodhall)
def numberSequence(aCount: Int, aNumberType: NumberType): Unit = {
println(s"\nThe first $aCount $aNumberType numbers are:")
numberInitialise()
(1 to aCount).foreach { _ =>
print(nextNumber(aNumberType).toString + " ")
}
println()
}
def primeSequence(aCount: Int, aNumberType: NumberType): Unit = {
println(s"\nThe indexes of the first $aCount $aNumberType primes are:")
primeInitialise()
@tailrec
def findPrimes(): Unit = {
if (count < aCount) {
if (nextNumber(aNumberType).isProbablePrime(Certainty)) {
print(primeIndex + " ")
count += 1
}
primeIndex += 1
findPrimes()
}
}
findPrimes()
println()
}
def nextNumber(aNumberType: NumberType): BigInteger = {
number = number.add(BigInteger.ONE)
power = power.shiftLeft(1)
aNumberType match {
case Cullen => number.multiply(power).add(BigInteger.ONE)
case Woodhall => number.multiply(power).subtract(BigInteger.ONE)
}
}
def numberInitialise(): Unit = {
number = BigInteger.ZERO
power = BigInteger.ONE
}
def primeInitialise(): Unit = {
count = 0
primeIndex = 1
numberInitialise()
}
}
- Output:
The first 20 Cullen numbers are: 3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521 The first 20 Woodhall numbers are: 1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519 The indexes of the first 5 Cullen primes are: 1 141 4713 5794 6611 The indexes of the first 12 Woodhall primes are: 2 3 6 30 75 81 115 123 249 362 384 462
Sidef
func cullen(n) { n * (1 << n) + 1 }
func woodall(n) { n * (1 << n) - 1 }
say "First 20 Cullen numbers:"
say cullen.map(1..20).join(' ')
say "\nFirst 20 Woodall numbers:"
say woodall.map(1..20).join(' ')
say "\nFirst 5 Cullen primes: (in terms of n)"
say 5.by { cullen(_).is_prime }.join(' ')
say "\nFirst 12 Woodall primes: (in terms of n)"
say 12.by { woodall(_).is_prime }.join(' ')
- Output:
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
Verilog
module main;
integer n, num;
initial begin
$display("First 20 Cullen numbers:");
for(n = 1; n <= 20; n=n+1)
begin
num = n * (2 ** n) + 1;
$write(num, " ");
end
$display("");
$display("First 20 Woodall numbers:");
for(n = 1; n <= 20; n=n+1)
begin
num = n * (2 ** n) - 1;
$write(num, " ");
end
$finish ;
end
endmodule
Wren
CLI
Cullen primes limited to first 2 as very slow after that.
import "./big" for BigInt
var cullen = Fn.new { |n| (BigInt.one << n) * n + 1 }
var woodall = Fn.new { |n| cullen.call(n) - 2 }
System.print("First 20 Cullen numbers (n * 2^n + 1):")
for (n in 1..20) System.write("%(cullen.call(n)) ")
System.print("\n\nFirst 20 Woodall numbers (n * 2^n - 1):")
for (n in 1..20) System.write("%(woodall.call(n)) ")
System.print("\n\nFirst 2 Cullen primes (in terms of n):")
var count = 0
var n = 1
while (count < 2) {
var cn = cullen.call(n)
if (cn.isProbablePrime(5)){
System.write("%(n) ")
count = count + 1
}
n = n + 1
}
System.print("\n\nFirst 12 Woodall primes (in terms of n):")
count = 0
n = 1
while (count < 12) {
var wn = woodall.call(n)
if (wn.isProbablePrime(5)){
System.write("%(n) ")
count = count + 1
}
n = n + 1
}
System.print()
- Output:
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 2 Cullen primes (in terms of n): 1 141 First 12 Woodall primes (in terms of n): 2 3 6 30 75 81 115 123 249 362 384 462
Embedded
Cullen primes still slow to emerge, just over 10 seconds overall.
/* Cullen_and_woodall_numbers_2.wren */
import "./gmp" for Mpz
var cullen = Fn.new { |n| (Mpz.one << n) * n + 1 }
var woodall = Fn.new { |n| cullen.call(n) - 2 }
System.print("First 20 Cullen numbers (n * 2^n + 1):")
for (n in 1..20) System.write("%(cullen.call(n)) ")
System.print("\n\nFirst 20 Woodall numbers (n * 2^n - 1):")
for (n in 1..20) System.write("%(woodall.call(n)) ")
System.print("\n\nFirst 5 Cullen primes (in terms of n):")
var count = 0
var n = 1
while (count < 5) {
var cn = cullen.call(n)
if (cn.probPrime(15) > 0){
System.write("%(n) ")
count = count + 1
}
n = n + 1
}
System.print("\n\nFirst 12 Woodall primes (in terms of n):")
count = 0
n = 1
while (count < 12) {
var wn = woodall.call(n)
if (wn.probPrime(15) > 0){
System.write("%(n) ")
count = count + 1
}
n = n + 1
}
System.print()
- Output:
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 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
XPL0
func Cullen(N);
int N;
return N<<N + 1;
func Woodall(N);
int N;
return N<<N - 1;
int I;
[Text(0, "First 20 Cullen numbers:^m^j");
for I:= 1 to 20 do
[IntOut(0, Cullen(I)); ChOut(0, ^ )];
CrLf(0);
CrLf(0);
Text(0, "First 20 Woodall numbers:^m^j");
for I:= 1 to 20 do
[IntOut(0, Woodall(I)); ChOut(0, ^ )];
CrLf(0);
]
- Output:
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
- Programming Tasks
- Solutions by Programming Task
- 11l
- ALGOL 68
- ALGOL 68-primes
- Arturo
- Asymptote
- AWK
- BASIC
- Applesoft BASIC
- BASIC256
- Chipmunk Basic
- FreeBASIC
- Gambas
- GW-BASIC
- PureBasic
- QBasic
- Run BASIC
- Tiny BASIC
- True BASIC
- MSX Basic
- XBasic
- Yabasic
- C++
- Delphi
- SysUtils,StdCtrls
- EasyLang
- F Sharp
- Factor
- FutureBasic
- Go
- GMP(Go wrapper)
- Haskell
- J
- Java
- Jq
- Julia
- Lua
- Mathematica
- Wolfram Language
- Maxima
- Nim
- Integers
- PARI/GP
- Perl
- Ntheory
- Phix
- Python
- Quackery
- Raku
- Ring
- RPL
- Ruby
- Rust
- Scala
- Sidef
- Verilog
- Wren
- Wren-big
- Wren-gmp
- XPL0