Cullen and Woodall numbers: Difference between revisions

Content added Content deleted
(J)
m (syntax highlighting fixup automation)
Line 44: Line 44:
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}}
<lang algol68>BEGIN # find Cullen and Woodall numbers and determine which are prime #
<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 93:
print( ( newline ) )
print( ( newline ) )
END
END
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 103: Line 103:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>cullen: function [n]->
<syntaxhighlight lang="rebol">cullen: function [n]->
inc n * 2^n
inc n * 2^n


Line 110: Line 110:


print ["First 20 cullen numbers:" join.with:" " to [:string] map 1..20 => cullen]
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]</lang>
print ["First 20 woodall numbers:" join.with:" " to [:string] map 1..20 => woodall]</syntaxhighlight>


{{out}}
{{out}}
Line 118: Line 118:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f CULLEN_AND_WOODALL_NUMBERS.AWK
# syntax: GAWK -f CULLEN_AND_WOODALL_NUMBERS.AWK
BEGIN {
BEGIN {
Line 135: Line 135:
exit(0)
exit(0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 145: Line 145:
==={{header|BASIC256}}===
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang BASIC256>print "First 20 Cullen numbers:"
<syntaxhighlight lang="basic256">print "First 20 Cullen numbers:"


for n = 1 to 20
for n = 1 to 20
Line 159: Line 159:
print int(num); " ";
print int(num); " ";
next n
next n
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 166: Line 166:


==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
<lang freebasic>Dim As Uinteger n, num
<syntaxhighlight lang="freebasic">Dim As Uinteger n, num
Print "First 20 Cullen numbers:"
Print "First 20 Cullen numbers:"


Line 180: Line 180:
Print num; " ";
Print num; " ";
Next n
Next n
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>First 20 Cullen numbers:
<pre>First 20 Cullen numbers:
Line 189: Line 189:


==={{header|PureBasic}}===
==={{header|PureBasic}}===
<lang PureBasic>OpenConsole()
<syntaxhighlight lang="purebasic">OpenConsole()
PrintN("First 20 Cullen numbers:")
PrintN("First 20 Cullen numbers:")


Line 205: Line 205:


PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
CloseConsole()</lang>
CloseConsole()</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 216: Line 216:
{{works with|True BASIC}}
{{works with|True BASIC}}
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang qbasic>DIM num AS LONG ''comment this line for True BASIC
<syntaxhighlight lang="qbasic">DIM num AS LONG ''comment this line for True BASIC
PRINT "First 20 Cullen numbers:"
PRINT "First 20 Cullen numbers:"


Line 232: Line 232:
PRINT num;
PRINT num;
NEXT n
NEXT n
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 241: Line 241:
{{works with|QBasic}}
{{works with|QBasic}}
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang qbasic>REM DIM num AS LONG !uncomment this line for QBasic
<syntaxhighlight lang="qbasic">REM DIM num AS LONG !uncomment this line for QBasic
PRINT "First 20 Cullen numbers:"
PRINT "First 20 Cullen numbers:"


Line 257: Line 257:
PRINT num;
PRINT num;
NEXT n
NEXT n
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 264: Line 264:


==={{header|Yabasic}}===
==={{header|Yabasic}}===
<lang yabasic>print "First 20 Cullen numbers:"
<syntaxhighlight lang="yabasic">print "First 20 Cullen numbers:"


for n = 1 to 20
for n = 1 to 20
Line 278: Line 278:
next n
next n
print
print
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 286: Line 286:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<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 293: Line 293:
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 304: Line 304:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
{{works with|Factor|0.99 2022-04-03}}
<lang factor>USING: arrays kernel math math.vectors prettyprint ranges
<syntaxhighlight lang="factor">USING: arrays kernel math math.vectors prettyprint ranges
sequences ;
sequences ;


20 [1..b] [ dup 2^ * 1 + ] map dup 2 v-n 2array simple-table.</lang>
20 [1..b] [ dup 2^ * 1 + ] map dup 2 v-n 2array simple-table.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 316: Line 316:
=={{header|Go}}==
=={{header|Go}}==
{{libheader|GMP(Go wrapper)}}
{{libheader|GMP(Go wrapper)}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 367: Line 367:
}
}
fmt.Println()
fmt.Println()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 385: Line 385:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>findCullen :: Int -> Integer
<syntaxhighlight lang="haskell">findCullen :: Int -> Integer
findCullen n = toInteger ( n * 2 ^ n + 1 )
findCullen n = toInteger ( n * 2 ^ n + 1 )


Line 399: Line 399:
print cullens
print cullens
putStrLn "First 20 Woodall numbers:"
putStrLn "First 20 Woodall numbers:"
print woodalls</lang>
print woodalls</syntaxhighlight>
{{out}}
{{out}}
<pre>First 20 Cullen numbers:
<pre>First 20 Cullen numbers:
Line 409: Line 409:
=={{header|J}}==
=={{header|J}}==


<lang J>cullen=: {{ y* 1+2x^y }}
<syntaxhighlight lang="j">cullen=: {{ y* 1+2x^y }}
woodall=: {{ y*_1+2x^y }}</lang>
woodall=: {{ y*_1+2x^y }}</syntaxhighlight>


Task example:
Task example:


<lang J> cullen 1+i.20
<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
3 10 27 68 165 390 903 2056 4617 10250 22539 49164 106509 229390 491535 1048592 2228241 4718610 9961491 20971540
woodall 1+i.20
woodall 1+i.20
1 6 21 60 155 378 889 2040 4599 10230 22517 49140 106483 229362 491505 1048560 2228207 4718574 9961453 20971500</lang>
1 6 21 60 155 378 889 2040 4599 10230 22517 49140 106483 229362 491505 1048560 2228207 4718574 9961453 20971500</syntaxhighlight>




=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Raku}}
{{trans|Raku}}
<lang julia>using Lazy
<syntaxhighlight lang="julia">using Lazy
using Primes
using Primes


Line 434: Line 434:
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
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
First 20 Cullen numbers: ( n × 2**n + 1)
First 20 Cullen numbers: ( n × 2**n + 1)
Line 449: Line 449:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>function T(t) return setmetatable(t, {__index=table}) end
<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.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
table.map = function(t,f) local s=T{} for i=1,#t do s[i]=f(t[i]) end return s end
Line 459: Line 459:
function woodall(n) return (n<<n)-1 end
function woodall(n) return (n<<n)-1 end
print("First 20 Woodall numbers:")
print("First 20 Woodall numbers:")
print(T{}:range(20):map(woodall):concat(" "))</lang>
print(T{}:range(20):map(woodall):concat(" "))</syntaxhighlight>
{{out}}
{{out}}
<pre>First 20 Cullen numbers:
<pre>First 20 Cullen numbers:
Line 467: Line 467:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[CullenNumber, WoodallNumber]
<syntaxhighlight lang="mathematica">ClearAll[CullenNumber, WoodallNumber]
SetAttributes[{CullenNumber, WoodallNumber}, Listable]
SetAttributes[{CullenNumber, WoodallNumber}, Listable]
CullenNumber[n_Integer] := n 2^n + 1
CullenNumber[n_Integer] := n 2^n + 1
Line 495: Line 495:
{i, 1, \[Infinity]}
{i, 1, \[Infinity]}
];
];
wps</lang>
wps</syntaxhighlight>
{{out}}
{{out}}
<pre>{3, 9, 25, 65, 161, 385, 897, 2049, 4609, 10241, 22529, 49153, 106497, 229377, 491521, 1048577, 2228225, 4718593, 9961473, 20971521}
<pre>{3, 9, 25, 65, 161, 385, 897, 2049, 4609, 10241, 22529, 49153, 106497, 229377, 491521, 1048577, 2228225, 4718593, 9961473, 20971521}
Line 504: Line 504:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use bigint;
use bigint;
Line 531: Line 531:
($m,$n) = (12,0);
($m,$n) = (12,0);
print "\n\nFirst $m Woodall primes: (in terms of n)\n";
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;</lang>
print do { $n < $m ? (!!is_prime(cullen $_,-1) and ++$n and "$_ ") : last } for 1 .. Inf;</syntaxhighlight>
{{out}}
{{out}}
<pre>First 20 Cullen numbers:
<pre>First 20 Cullen numbers:
Line 546: Line 546:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<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 595: Line 595:
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 607: Line 607:


=={{header|Python}}==
=={{header|Python}}==
<lang python>
<syntaxhighlight lang="python">
print("working...")
print("working...")
print("First 20 Cullen numbers:")
print("First 20 Cullen numbers:")
Line 624: Line 624:
print()
print()
print("done...")
print("done...")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 636: Line 636:
===Bit Shift===
===Bit Shift===
{{trans|Quackery}}
{{trans|Quackery}}
<lang Python>def cullen(n): return((n<<n)+1)
<syntaxhighlight lang="python">def cullen(n): return((n<<n)+1)
def woodall(n): return((n<<n)-1)
def woodall(n): return((n<<n)-1)
Line 648: Line 648:
for i in range(1,20):
for i in range(1,20):
print(woodall(i),end=" ")
print(woodall(i),end=" ")
print()</lang>
print()</syntaxhighlight>


{{out}}
{{out}}
Line 655: Line 655:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ dup << 1+ ] is cullen ( n --> n )
<syntaxhighlight lang="quackery"> [ dup << 1+ ] is cullen ( n --> n )


[ dup << 1 - ] is woodall ( n --> n )
[ dup << 1 - ] is woodall ( n --> n )
Line 663: Line 663:
cr
cr
say "First 20 Woodall numbers:" cr
say "First 20 Woodall numbers:" cr
20 times [ i^ 1+ woodall echo sp ] cr</lang>
20 times [ i^ 1+ woodall echo sp ] cr</syntaxhighlight>


{{out}}
{{out}}
Line 676: Line 676:
=={{header|Raku}}==
=={{header|Raku}}==


<lang perl6>my @cullen = ^∞ .map: { $_ × 1 +< $_ + 1 };
<syntaxhighlight lang="raku" line>my @cullen = ^∞ .map: { $_ × 1 +< $_ + 1 };
my @woodall = ^∞ .map: { $_ × 1 +< $_ - 1 };
my @woodall = ^∞ .map: { $_ × 1 +< $_ - 1 };


Line 682: Line 682:
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</lang>
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 697: Line 697:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"


Line 716: Line 716:


see nl + "done..." + nl
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 729: Line 729:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>// [dependencies]
<syntaxhighlight lang="rust">// [dependencies]
// rug = "1.15.0"
// rug = "1.15.0"


Line 773: Line 773:
.collect();
.collect();
println!("{}", woodall_primes.join(" "));
println!("{}", woodall_primes.join(" "));
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 791: Line 791:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func cullen(n) { n * (1 << n) + 1 }
<syntaxhighlight lang="ruby">func cullen(n) { n * (1 << n) + 1 }
func woodall(n) { n * (1 << n) - 1 }
func woodall(n) { n * (1 << n) - 1 }


Line 804: Line 804:


say "\nFirst 12 Woodall primes: (in terms of n)"
say "\nFirst 12 Woodall primes: (in terms of n)"
say 12.by { woodall(_).is_prime }.join(' ')</lang>
say 12.by { woodall(_).is_prime }.join(' ')</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 821: Line 821:


=={{header|Verilog}}==
=={{header|Verilog}}==
<lang Verilog>module main;
<syntaxhighlight lang="verilog">module main;
integer n, num;
integer n, num;
Line 840: Line 840:
$finish ;
$finish ;
end
end
endmodule</lang>
endmodule</syntaxhighlight>




Line 847: Line 847:
{{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.
<lang ecmascript>import "./big" for BigInt
<syntaxhighlight lang="ecmascript">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 882: Line 882:
n = n + 1
n = n + 1
}
}
System.print()</lang>
System.print()</syntaxhighlight>


{{out}}
{{out}}
Line 902: Line 902:
{{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.
<lang ecmascript>/* cullen_and_woodall_numbers2.wren */
<syntaxhighlight lang="ecmascript">/* cullen_and_woodall_numbers2.wren */


import "./gmp" for Mpz
import "./gmp" for Mpz
Line 939: Line 939:
n = n + 1
n = n + 1
}
}
System.print()</lang>
System.print()</syntaxhighlight>


{{out}}
{{out}}