Ultra useful primes: Difference between revisions

m
syntax highlighting fixup automation
m (J)
m (syntax highlighting fixup automation)
Line 25:
Uses Algol 68G's LONG LONG INT which has programmer-specifiable precision. Uses Miller Rabin primality testing.
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find members of the sequence a(n) = smallest k such that 2^(2^n) - k is prime #
PR precision 650 PR # set number of digits for LONG LOMG INT #
# 2^(2^10) has 308 digits but we need more for #
Line 42:
DO SKIP OD
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 50:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">ultraUseful: function [n][
k: 1
p: (2^2^n) - k
Line 63:
print repeat "-" 10
loop 1..10 'x ->
print [(pad to :string x 3) "|" (pad.right to :string ultraUseful x 4)]</langsyntaxhighlight>
 
{{out}}
Line 82:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: io kernel lists lists.lazy math math.primes prettyprint ;
 
: useful ( -- list )
Line 88:
[ 2^ 2^ 1 lfrom [ - prime? ] with lfilter car ] lmap-lazy ;
 
10 useful ltake [ pprint bl ] leach nl</langsyntaxhighlight>
{{out}}
<pre>
Line 96:
=={{header|Go}}==
{{libheader|GMP(Go wrapper)}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 123:
fmt.Printf("%2d %d\n", n, a(n))
}
}</langsyntaxhighlight>
 
{{out}}
Line 148:
Implementation:
 
<langsyntaxhighlight Jlang="j">uup=: {{
ref=. 2x^2^y+1
k=. 1
while. -. 1 p: ref-k do. k=. k+2 end.
}}"0</langsyntaxhighlight>
 
I don't have the patience to get this little laptop to compute the first 10 such elements, so here I only show the first five:
 
<langsyntaxhighlight Jlang="j"> uup i.5
1 3 5 15 5</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
nearpow2pow2prime(n) = findfirst(k -> isprime(2^(big"2"^n) - k), 1:10000)
 
@time println([nearpow2pow2prime(n) for n in 1:12])
</langsyntaxhighlight>{{out}}
<pre>
[1, 3, 5, 15, 5, 59, 159, 189, 569, 105, 1557, 2549]
Line 172:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[FindUltraUsefulPrimeK]
FindUltraUsefulPrimeK[n_] := Module[{num, tmp},
num = 2^(2^n);
Line 186:
]
res = FindUltraUsefulPrimeK /@ Range[13];
TableForm[res, TableHeadings -> Automatic]</langsyntaxhighlight>
{{out}}
<pre>1 1
Line 202:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 220:
}
 
say join ' ', useful 1..13;</langsyntaxhighlight>
{{out}}
<pre>1 3 5 15 5 59 159 189 569 105 1557 2549 2439</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 252:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 262:
The first 10 take less than a quarter second. 11 through 13, a little under 30 seconds. Drops off a cliff after that.
 
<syntaxhighlight lang="raku" perl6line>sub useful ($n) {
(|$n).map: {
my $p = 1 +< ( 1 +< $_ );
Line 271:
put useful 1..10;
 
put useful 11..13;</langsyntaxhighlight>
{{out}}
<pre>1 3 5 15 5 59 159 189 569 105
Line 277:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">say(" n k")
say("----------")
 
Line 283:
var t = 2**(2**n)
printf("%2d %d\n", n, {|k| t - k -> is_prob_prime }.first)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 309:
{{libheader|Wren-fmt}}
Manages to find the first ten but takes 84 seconds to do so.
<langsyntaxhighlight lang="ecmascript">import "./big" for BigInt
import "./fmt" for Fmt
 
Line 327:
System.print(" n k")
System.print("----------")
for (n in 1..10) Fmt.print("$2d $d", n, a.call(n))</langsyntaxhighlight>
 
{{out}}
Line 347:
{{libheader|Wren-gmp}}
The following takes about 17 seconds to get to n = 13 but 7 minutes 10 seconds to reach n = 14. I didn't bother after that.
<langsyntaxhighlight lang="ecmascript">import "./gmp" for Mpz
import "./fmt" for Fmt
 
Line 365:
System.print(" n k")
System.print("----------")
for (n in 1..14) Fmt.print("$2d $d", n, a.call(n))</langsyntaxhighlight>
 
{{out}}
10,333

edits