Consecutive primes with ascending or descending differences: Difference between revisions
Content added Content deleted
(→{{header|Lua}}: added Lua solution) |
(GP) |
||
Line 634: | Line 634: | ||
First longest sequence of consecutive primes with descending differences: |
First longest sequence of consecutive primes with descending differences: |
||
322171 (22) 322193 (20) 322213 (16) 322229 (8) 322237 (6) 322243 (4) 322247 (2) 322249</pre> |
322171 (22) 322193 (20) 322213 (16) 322229 (8) 322237 (6) 322243 (4) 322247 (2) 322249</pre> |
||
=={{header|Pari/GP}}== |
|||
Code is pretty reasonable, runs in ~70 ms at 1,000,000. Running under PARI (with gp2c translation) could take advantage of the diff structure of the prime table directly for small cases and avoid substantial overhead, gaining at least a factor of 2 in performance. |
|||
<lang parigp>showPrecPrimes(p, n)= |
|||
{ |
|||
my(v=vector(n)); |
|||
v[n]=p; |
|||
forstep(i=n-1,1,-1, |
|||
v[i]=precprime(v[i+1]-1) |
|||
); |
|||
for(i=1,n, print1(v[i]" ")); |
|||
} |
|||
list(lim)= |
|||
{ |
|||
my(p=3,asc,dec,ar,dr,arAt=3,drAt=3,last=2); |
|||
forprime(q=5,lim, |
|||
my(g=q-p); |
|||
if(g<last, |
|||
asc=0; |
|||
if(desc++>dr, |
|||
dr=desc; |
|||
drAt=q |
|||
) |
|||
,g>last, |
|||
desc=0; |
|||
if(asc++>ar, |
|||
ar=asc; |
|||
arAt=q |
|||
) |
|||
, |
|||
asc=desc=0 |
|||
); |
|||
p=q; |
|||
last=g |
|||
); |
|||
print("Descending differences:"); |
|||
showPrecPrimes(drAt, dr+2); |
|||
print("\nAscending differences:"); |
|||
showPrecPrimes(arAt, ar+2); |
|||
} |
|||
list(10^6)</lang> |
|||
{{out}} |
|||
<pre>Descending differences: |
|||
322171 322193 322213 322229 322237 322243 322247 322249 |
|||
Ascending differences: |
|||
128981 128983 128987 128993 129001 129011 129023 129037</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |