Consecutive primes with ascending or descending differences: Difference between revisions
Consecutive primes with ascending or descending differences (view source)
Revision as of 22:01, 26 August 2022
, 1 year agosyntax highlighting fixup automation
(J: same conceptual algorithm, faster, more compact) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 15:
{{trans|Python}}
<
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 65:
pindex++
print(longest_list)</
{{out}}
Line 74:
=={{header|ALGOL 68}}==
<
# are strictly ascending/descending #
# reurns a list of primes up to n #
Line 147:
show sequence( primes, "ascending", asc start, asc length );
show sequence( primes, "descending", desc start, desc length )
END</
{{out}}
<pre>
Line 161:
=={{header|C sharp|C#}}==
Extended the limit up to see what would happen.
<
using System.Collections.Generic;
using TG = System.Tuple<int, int>;
Line 223:
}
}
}</
{{out}}
<pre>For primes up to 1,000,000:
Line 257:
=={{header|C++}}==
{{libheader|Primesieve}}
<
#include <iostream>
#include <vector>
Line 309:
print_diffs(v);
return 0;
}</
{{out}}
Line 325:
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
=={{header|F_Sharp|F#}}==
<
// Longest ascending and decending sequences of difference between consecutive primes: Nigel Galloway. April 5th., 2021
let fN g fW=primes32()|>Seq.takeWhile((>)g)|>Seq.pairwise|>Seq.fold(fun(n,i,g)el->let w=fW el in match w>n with true->(w,el::i,g) |_->(w,[el],if List.length i>List.length g then i else g))(0,[],[])
for i in [1;2;6;12;18;100] do let _,_,g=fN(i*1000000)(fun(n,g)->g-n) in printfn "Longest ascending upto %d000000->%d:" i (g.Length+1); g|>List.rev|>List.iter(fun(n,g)->printf "%d (%d) %d " n (g-n) g); printfn ""
let _,_,g=fN(i*1000000)(fun(n,g)->n-g) in printfn "Longest decending upto %d000000->%d:" i (g.Length+1); g|>List.rev|>List.iter(fun(n,g)->printf "%d (%d) %d " n (g-n) g); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 361:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
math.primes math.statistics sequences sequences.extras
tools.memory.private ;
Line 388:
printf [ .run ] each ; inline
[ < ] [ > ] [ .runs nl ] bi@</
{{out}}
<pre>
Line 403:
=={{header|FreeBASIC}}==
Use any of the primality testing code on this site as an include; I won't reproduce it here.
<
#include"isprime.bas"
Line 458:
print prime(i-1);" (";prime(i)-prime(i-1);") ";
next i
print prime(i-1)</
{{out}}
<pre>The longest sequence of ascending primes (with their difference from the last one) is:
Line 468:
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 521:
longestSeq(dir)
}
}</
{{out}}
Line 538:
=={{header|Haskell}}==
<
-- generates consecutive subsequences defined by given equivalence relation
Line 562:
differences $
takeWhile (< n) primes
differences l = zip l $ zipWith (-) (tail l) l</
<pre>λ> reverse <$> consecutives (<) [1,0,1,2,3,4,3,2,3,4,5,6,7,6,5,4,3,4,5]
Line 574:
=={{header|J}}==
<
128981 128983 128987 128993 129001 129011 129023 129037
;{.(\: #@>) tris <@~.@,;._1~ >:/ 2 -/\ |: tris=: 3 ]\ p: i. p:inv 1e6
322171 322193 322213 322229 322237 322243 322247 322249</
=={{header|jq}}==
Line 589:
'''Preliminaries'''
<
def add(s): reduce s as $x (null; .+$x);
Line 598:
else 2, (range(3; $n) | select(is_prime))
end;
</syntaxhighlight>
'''The Task'''
<
# Output: informative strings
def longestSequences:
Line 639:
"For primes < 1 million:",
( 1E6 | longestSequences )</
{{out}}
<pre>
Line 654:
=={{header|Julia}}==
<
function primediffseqs(maxnum = 1_000_000)
Line 683:
primediffseqs()
</
<pre>
Ascending: [128981, 128983, 128987, 128993, 129001, 129011, 129023, 129037] Diffs: [2, 4, 6, 8, 10, 12, 14]
Line 691:
=={{header|Lua}}==
This task uses <code>primegen</code> from: [[Extensible_prime_generator#Lua]]
<
local currlist = {primelist[1]}
local longlist, prevdiff = currlist, 0
Line 713:
print("ASC ("..#cplist.."): ["..table.concat(cplist, " ").."]")
cplist = findcps(primegen.primelist, function(a,b) return a<b end)
print("DESC ("..#cplist.."): ["..table.concat(cplist, " ").."]")</
{{out}}
<pre>ASC (8): [128981 128983 128987 128993 129001 129011 129023 129037]
Line 719:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
s = Split[Differences[prime], Less];
max = Max[Length /@ s];
Line 730:
diffs = Select[s, Length/*EqualTo[max]];
seqs = SequencePosition[Flatten[s], #, 1][[1]] & /@ diffs;
Take[prime, # + {0, 1}] & /@ seqs</
{{out}}
<pre>128981 128983 128987 128993 129001 129011 129023 129037
Line 740:
=={{header|Nim}}==
<
const N = 1_000_000
Line 801:
echo()
echo "First longest sequence of consecutive primes with descending differences:"
echo longestSeq(Descending)</
{{out}}
Line 814:
=={{header|Pari/GP}}==
Code is pretty reasonable, runs in ~70 ms at 1,000,000. Running under PARI (starting from 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.
<
{
my(v=vector(n));
Line 851:
showPrecPrimes(arAt, ar+2);
}
list(10^6)</
{{out}}
<pre>Descending differences:
Line 861:
{{trans|Raku}}
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 890:
say "Longest run(s) of ascending prime gaps up to $limit:\n" . join "\n", runs('>');
say "\nLongest run(s) of descending prime gaps up to $limit:\n" . join "\n", runs('<');</
{{out}}
<pre>Longest run(s) of ascending prime gaps up to 1000000:
Line 902:
=={{header|Phix}}==
<!--<
<span style="color: #004080;">integer</span> <span style="color: #000000;">pn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- prime numb</span>
<span style="color: #000000;">lp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- last prime</span>
Line 929:
<span style="color: #0000FF;">{{</span><span style="color: #008000;">"ascending"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"descending"</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">run</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">length</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;">g</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 937:
=={{header|Python}}==
<
from sympy import sieve
Line 987:
print(longest_list)
</syntaxhighlight>
{{out}}
Line 997:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
use Lingua::EN::Numbers;
Line 1,028:
say "Longest run(s) of ascending prime gaps up to {comma $limit}:\n" ~ runs(&infix:«>»);
say "\nLongest run(s) of descending prime gaps up to {comma $limit}:\n" ~ runs(&infix:«<»);</
{{out}}
<pre>Longest run(s) of ascending prime gaps up to 1,000,000:
Line 1,040:
=={{header|REXX}}==
<
/*──────────── between the primes are strictly ascending; also for strictly descending.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
Line 1,103:
if $\=='' then say center(idx, 7)"│" substr($, 2) /*maybe show residual output*/
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say; say commas(Cprimes) ' was the'title; return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,124:
=={{header|Ruby}}==
<
limit = 1_000_000
Line 1,130:
p Prime.each(limit).each_cons(2).chunk_while{|(i1,i2), (j1,j2)| j1-i1 < j2-i2 }.max_by(&:size).flatten.uniq
puts "\nFirst found longest run of descending prime gaps up to #{limit}:"
p Prime.each(limit).each_cons(2).chunk_while{|(i1,i2), (j1,j2)| j1-i1 > j2-i2 }.max_by(&:size).flatten.uniq</
{{out}}
<pre>First found longest run of ascending prime gaps up to 1000000:
Line 1,140:
=={{header|Rust}}==
<
// primal = "0.3"
Line 1,198:
print_diffs(&v);
}
}</
{{out}}
Line 1,214:
=={{header|Sidef}}==
{{trans|Raku}}
<
var run = 0
Line 1,244:
say "\nLongest run(s) of descending prime gaps up to #{limit.commify}:"
say runs({|a,b| a < b }, primes).join("\n")</
{{out}}
<pre>
Line 1,259:
=={{header|Wren}}==
{{libheader|Wren-math}}
<
var LIMIT = 999999
Line 1,294:
System.print("For primes < 1 million:\n")
for (dir in ["ascending", "descending"]) longestSeq.call(dir)</
{{out}}
Line 1,312:
=={{header|XPL0}}==
<
int N, I;
[if (N&1) = 0 \even number\ then return false;
Line 1,360:
[ShowSeq(+1, "ascending"); \main
ShowSeq(-1, "descending");
]</
{{out}}
|