Quad-power prime seeds: Difference between revisions

Content deleted Content added
Wherrera (talk | contribs)
julia example
Peak (talk | contribs)
 
(One intermediate revision by one other user not shown)
Line 553:
2 million is 2015496 at index 234
3 million is 3005316 at index 319
</pre>
 
=={{header|jq}}==
'''Works with gojq, the Go implementation of jq'''
 
gojq supports unbounded-precision integer arithmetic,
so the results using gojq are presented.
The program presented here does, however, run using the C or Rust
implementations of jq, but accuracy is lost after 48,321,
 
Since an exact test of primality is used, things slow to a snail's pace
after the first 10 or so qpps are found.
<syntaxhighlight lang="jq">
# The following may be omitted if using the C or Rust implementations of jq
def _nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
 
# tabular print
def tprint($columns; $width):
reduce _nwise($columns) as $row ("";
. + ($row|map(lpad($width)) | join(" ")) + "\n" );
 
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 sqrt as $s
| 23
| until( . > $s or ($n % . == 0); . + 2)
| . > $s
end;
 
def quad_power_primes:
range(1; infinite)
| . as $n
| (reduce range(1;5) as $i ([range(0;5) | 1];
.[$i] = $n * .[$i-1])) as $powers
| select(all(1, 2, 3, 4;
$powers[.] + $n + 1 | is_prime) ) ;
 
def qpp($n):
"The first \($n) quad-power prime seeds:",
( [limit($n; quad_power_primes)]
| tprint(10; 8) );
 
# qpp(50) # too slow
qpp(27)
</syntaxhighlight>
{{output}}
<pre>
The first 27 quad-power prime seeds:
1 2 5 6 69 131 426 1665 2129 2696
5250 7929 9689 13545 14154 14286 16434 19760 25739 27809
29631 36821 41819 46619 48321 59030 60500
</pre>
 
Line 842 ⟶ 906:
77685 79646 80535 82655 85251 86996 91014 96566 97739 105939
108240 108681 119754 122436 123164 126489 140636 150480 153179 163070
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var qpps = (1..Inf -> by(2).lazy.grep { .is_prime }.map {|n| (n-1)>>1 }.grep {|n|
is_prime(n**2 + n + 1) && all_prime(n**3 + n + 1, n**4 + n + 1)
})
 
with (50) {|n|
say "First #{n} quad-power prime seeds:"
qpps.first(n).each_slice(10, {|*s| say s.map{ '%6s' % _ }.join(' ') })
}</syntaxhighlight>
{{out}}
<pre>
First 50 quad-power prime seeds:
1 2 5 6 69 131 426 1665 2129 2696
5250 7929 9689 13545 14154 14286 16434 19760 25739 27809
29631 36821 41819 46619 48321 59030 60500 61955 62321 73610
77685 79646 80535 82655 85251 86996 91014 96566 97739 105939
108240 108681 119754 122436 123164 126489 140636 150480 153179 163070
</pre>
 
Line 848 ⟶ 931:
{{libheader|Wren-fmt}}
GMP allows us to stretch a little more.
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpz
import "./fmt" for Fmt