Cullen and Woodall numbers: Difference between revisions

Add Scala implementation
(Added 11l)
(Add Scala implementation)
 
(27 intermediate revisions by 14 users not shown)
Line 147:
<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}}==
Line 174 ⟶ 192:
 
=={{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:"
Line 192 ⟶ 231:
end</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
<pre>
 
Igual que la entrada de FreeBASIC.
==={{header|Chipmunk Basic}}===
</pre>
{{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="freebasicvb">Dim As Uinteger n, num
Print "First 20 Cullen numbers:"
 
Line 218 ⟶ 279:
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}}===
Line 238 ⟶ 325:
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|QBasic}}===
Line 265 ⟶ 350:
END</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
<pre>
 
Igual que la entrada de FreeBASIC.
==={{header|Run BASIC}}===
</pre>
{{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}}===
Line 290 ⟶ 460:
END</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
<pre>
 
Igual que la entrada de FreeBASIC.
==={{header|MSX Basic}}===
</pre>
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="yabasicbasic">print "First 20 Cullen numbers:"
 
for n = 1 to 20
Line 311 ⟶ 512:
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:
Igual que la entrada de FreeBASIC.
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#}}==
Line 344 ⟶ 660:
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}}==
Line 449 ⟶ 808:
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>
 
 
Line 532 ⟶ 1,087:
{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}}==
Line 660 ⟶ 1,345:
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>
Line 757 ⟶ 1,442:
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>
 
Line 819 ⟶ 1,547:
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>
 
Line 878 ⟶ 1,697:
{{libheader|Wren-big}}
Cullen primes limited to first 2 as very slow after that.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
 
var cullen = Fn.new { |n| (BigInt.one << n) * n + 1 }
Line 933 ⟶ 1,752:
{{libheader|Wren-gmp}}
Cullen primes still slow to emerge, just over 10 seconds overall.
<syntaxhighlight lang="ecmascriptwren">/* cullen_and_woodall_numbers2Cullen_and_woodall_numbers_2.wren */
 
import "./gmp" for Mpz
Line 985 ⟶ 1,804:
First 12 Woodall primes (in terms of n):
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>
337

edits