Consecutive primes with ascending or descending differences: Difference between revisions

m
syntax highlighting fixup automation
(J: same conceptual algorithm, faster, more compact)
m (syntax highlighting fixup automation)
Line 15:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 65:
pindex++
 
print(longest_list)</langsyntaxhighlight>
 
{{out}}
Line 74:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # find sequences of primes where the gaps between the elements #
# 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</langsyntaxhighlight>
{{out}}
<pre>
Line 161:
=={{header|C sharp|C#}}==
Extended the limit up to see what would happen.
<langsyntaxhighlight lang="csharp">using System.Linq;
using System.Collections.Generic;
using TG = System.Tuple<int, int>;
Line 223:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>For primes up to 1,000,000:
Line 257:
=={{header|C++}}==
{{libheader|Primesieve}}
<langsyntaxhighlight lang="cpp">#include <cstdint>
#include <iostream>
#include <vector>
Line 309:
print_diffs(v);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 325:
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{out}}
<pre>
Line 361:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: arrays assocs formatting grouping io kernel literals math
math.primes math.statistics sequences sequences.extras
tools.memory.private ;
Line 388:
printf [ .run ] each ; inline
 
[ < ] [ > ] [ .runs nl ] bi@</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="freebasic">#define UPPER 1000000
#include"isprime.bas"
 
Line 458:
print prime(i-1);" (";prime(i)-prime(i-1);") ";
next i
print prime(i-1)</langsyntaxhighlight>
{{out}}
<pre>The longest sequence of ascending primes (with their difference from the last one) is:
Line 468:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 521:
longestSeq(dir)
}
}</langsyntaxhighlight>
 
{{out}}
Line 538:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes (primes)
 
-- generates consecutive subsequences defined by given equivalence relation
Line 562:
differences $
takeWhile (< n) primes
differences l = zip l $ zipWith (-) (tail l) l</langsyntaxhighlight>
 
<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}}==
<langsyntaxhighlight Jlang="j"> ;{.(\: #@>) tris <@~.@,;._1~ <:/ 2 -/\ |: tris=: 3 ]\ p: i. p:inv 1e6
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</langsyntaxhighlight>
 
=={{header|jq}}==
Line 589:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq"># For streams of strings or of arrays or of numbers:
def add(s): reduce s as $x (null; .+$x);
 
Line 598:
else 2, (range(3; $n) | select(is_prime))
end;
</syntaxhighlight>
</lang>
'''The Task'''
<langsyntaxhighlight lang="jq"># Input: null or limit+1
# Output: informative strings
def longestSequences:
Line 639:
"For primes < 1 million:",
( 1E6 | longestSequences )</langsyntaxhighlight>
{{out}}
<pre>
Line 654:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function primediffseqs(maxnum = 1_000_000)
Line 683:
 
primediffseqs()
</langsyntaxhighlight>{{out}}
<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]]
<langsyntaxhighlight lang="lua">function findcps(primelist, fcmp)
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, " ").."]")</langsyntaxhighlight>
{{out}}
<pre>ASC (8): [128981 128983 128987 128993 129001 129011 129023 129037]
Line 719:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">prime = Prime[Range[PrimePi[10^6]]];
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</langsyntaxhighlight>
{{out}}
<pre>128981 128983 128987 128993 129001 129011 129023 129037
Line 740:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strformat, sugar
 
const N = 1_000_000
Line 801:
echo()
echo "First longest sequence of consecutive primes with descending differences:"
echo longestSeq(Descending)</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="parigp">showPrecPrimes(p, n)=
{
my(v=vector(n));
Line 851:
showPrecPrimes(arAt, ar+2);
}
list(10^6)</langsyntaxhighlight>
{{out}}
<pre>Descending differences:
Line 861:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
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('<');</langsyntaxhighlight>
{{out}}
<pre>Longest run(s) of ascending prime gaps up to 1000000:
Line 902:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 937:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
from sympy import sieve
 
Line 987:
print(longest_list)
</syntaxhighlight>
</lang>
 
{{out}}
Line 997:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Math::Primesieve;
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:«<»);</langsyntaxhighlight>
{{out}}
<pre>Longest run(s) of ascending prime gaps up to 1,000,000:
Line 1,040:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds the longest sequence of consecutive primes where the differences */
/*──────────── 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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,124:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
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</langsyntaxhighlight>
{{out}}
<pre>First found longest run of ascending prime gaps up to 1000000:
Line 1,140:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
 
Line 1,198:
print_diffs(&v);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,214:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func runs(f, arr) {
 
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")</langsyntaxhighlight>
{{out}}
<pre>
Line 1,259:
=={{header|Wren}}==
{{libheader|Wren-math}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
 
var LIMIT = 999999
Line 1,294:
 
System.print("For primes < 1 million:\n")
for (dir in ["ascending", "descending"]) longestSeq.call(dir)</langsyntaxhighlight>
 
{{out}}
Line 1,312:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N > 2 is a prime number
int N, I;
[if (N&1) = 0 \even number\ then return false;
Line 1,360:
[ShowSeq(+1, "ascending"); \main
ShowSeq(-1, "descending");
]</langsyntaxhighlight>
 
{{out}}
10,333

edits