Ultra useful primes: Difference between revisions
(→{{header|Factor}}: simplify) |
|||
Line 58: | Line 58: | ||
{{out}} |
{{out}} |
||
<pre>1 3 5 15 5 59 159 189 569 105 1557 2549 2439</pre> |
<pre>1 3 5 15 5 59 159 189 569 105 1557 2549 2439</pre> |
||
=={{header|Phix}}== |
|||
<!--<lang 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> |
|||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
|||
<span style="color: #004080;">mpz</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span> |
|||
<span style="color: #7060A8;">mpz_sub_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
|||
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #000000;">k</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span> |
|||
<span style="color: #7060A8;">mpz_sub_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #000000;">k</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</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;">"%d "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">64</span> <span style="color: #008080;">then</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: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">11</span> <span style="color: #008080;">to</span> <span style="color: #000000;">13</span> <span style="color: #008080;">do</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;">"%d "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<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> |
|||
<!--</lang>--> |
|||
{{out}} |
|||
<pre> |
|||
1 3 5 15 5 59 159 189 569 105 "0.0s" |
|||
1557 2549 2439 "1 minute and 1s" |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
Revision as of 06:38, 14 January 2022
An ultra-useful prime is a member of the sequence where each a(n) is the smallest positive integer k such that 2(2n) - k is prime.
k must always be an odd number since 2 to any power is always even.
- Task
- Find and show here, on this page, the first 10 elements of the sequence.
- Stretch
- Find and show the next several elements. (The numbers get really big really fast. Only nineteen elements have been identified as of this writing.)
- See also
Factor
<lang factor>USING: io kernel lists lists.lazy math math.primes prettyprint ;
- useful ( -- list )
1 lfrom [ 2^ 2^ 1 lfrom [ - prime? ] with lfilter car ] lmap-lazy ;
10 useful ltake [ pprint bl ] leach nl</lang>
- Output:
1 3 5 15 5 59 159 189 569 105
Perl
<lang perl>use strict; use warnings; use feature 'say'; use bigint; use ntheory 'is_prime';
sub useful {
my @n = @_; my @u; for my $n (@n) { my $p = 2**(2**$n); LOOP: for (my $k = 1; $k < $p; $k += 2) { is_prime($p-$k) and push @u, $k and last LOOP; } } @u
}
say join ' ', useful 1..13;</lang>
- Output:
1 3 5 15 5 59 159 189 569 105 1557 2549 2439
Phix
with javascript_semantics atom t0 = time() include mpfr.e mpz p = mpz_init() function a(integer n) mpz_ui_pow_ui(p,2,power(2,n)) mpz_sub_si(p,p,1) integer k = 1 while not mpz_prime(p) do k += 2 mpz_sub_si(p,p,2) end while return k end function for i=1 to 10 do printf(1,"%d ",a(i)) end for if machine_bits()=64 then ?elapsed(time()-t0) for i=11 to 13 do printf(1,"%d ",a(i)) end for end if ?elapsed(time()-t0)
- Output:
1 3 5 15 5 59 159 189 569 105 "0.0s" 1557 2549 2439 "1 minute and 1s"
Raku
The first 10 take less than a quarter second. 11 through 13, a little under 30 seconds. Drops off a cliff after that.
<lang perl6>sub useful ($n) {
(|$n).map: { my $p = 1 +< ( 1 +< $_ ); ^$p .first: ($p - *).is-prime }
}
put useful 1..10;
put useful 11..13;</lang>
- Output:
1 3 5 15 5 59 159 189 569 105 1557 2549 2439
Wren
An embedded version as Wren-CLI (with BigInt) will be very slow for this task.
The following takes about 17.5 seconds to get to n = 13 but 7 minutes 15 seconds to reach n = 14. I didn't bother after that. <lang ecmascript>import "./gmp" for Mpz import "./fmt" for Fmt
var a = Fn.new { |n|
var p = Mpz.one.lsh(1 << n) var k = 1 while (true) { var q = p - k if (q.probPrime(15) > 0) return k k = k + 2 }
}
System.print(" n k") System.print("----------") for (n in 1..14) Fmt.print("$2d $d", n, a.call(n))</lang>
- Output:
n k ---------- 1 1 2 3 3 5 4 15 5 5 6 59 7 159 8 189 9 569 10 105 11 1557 12 2549 13 2439 14 13797