Consecutive primes with ascending or descending differences: Difference between revisions

Added Easylang
(Added C)
(Added Easylang)
 
(5 intermediate revisions by 4 users not shown)
Line 158:
322171 (22) 322193 (20) 322213 (16) 322229 (8) 322237 (6) 322243 (4) 322247 (2) 322249
</pre>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
Use any of the primality testing code on this site as an include; I won't reproduce it here.
<syntaxhighlight lang="freebasic">#define UPPER 1000000
#include"isprime.bas"
 
dim as uinteger champ = 0, record = 0, streak, i, j, n
 
'first generate all the primes below UPPER
redim as uinteger prime(1 to 2)
prime(1) = 2 : prime(2) = 3
for i = 5 to UPPER step 2
if isprime(i) then
redim preserve prime(1 to ubound(prime) + 1)
prime(ubound(prime)) = i
end if
next i
n = ubound(prime)
 
'now look for the longest streak of ascending primes
for i = 2 to n-1
j = i + 1
streak = 1
while j<=n andalso prime(j)-prime(j-1) > prime(j-1)-prime(j-2)
streak += 1
j+=1
wend
if streak > record then
champ = i-1
record = streak
end if
next i
 
print "The longest sequence of ascending primes (with their difference from the last one) is:"
for i = champ+1 to champ+record
print prime(i-1);" (";prime(i)-prime(i-1);") ";
next i
print prime(i-1) : print
'now for the descending ones
 
record = 0 : champ = 0
for i = 2 to n-1
j = i + 1
streak = 1
while j<=n andalso prime(j)-prime(j-1) < prime(j-1)-prime(j-2) 'identical to above, but for the inequality sign
streak += 1
j+=1
wend
if streak > record then
champ = i-1
record = streak
end if
next i
 
print "The longest sequence of descending primes (with their difference from the last one) is:"
for i = champ+1 to champ+record
print prime(i-1);" (";prime(i)-prime(i-1);") ";
next i
print prime(i-1)</syntaxhighlight>
{{out}}
<pre>The longest sequence of ascending primes (with their difference from the last one) is:
128981 (2) 128983 (4) 128987 (6) 128993 (8) 129001 (10) 129011 (12) 129023 (14) 129037
 
The longest sequence of descending primes (with their difference from the last one) is:
322171 (22) 322193 (20) 322213 (16) 322229 (8) 322237 (6) 322243 (4) 322247 (2) 322249</pre>
 
=={{header|C}}==
Line 427 ⟶ 493:
 
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
The code makes heavy use of "Sieve" object developed for other Rosetta Code tasks. The Sieve object is described here: [[Extensible_prime_generator#Delphi]]
 
<syntaxhighlight lang="Delphi">
 
 
procedure LongestAscendDescend(Memo: TMemo);
{Find the longest Ascending and Descending sequences of primes}
var I: integer;
var Sieve: TPrimeSieve;
 
procedure FindLongestSequence(Ascend: boolean);
{Find the longest sequence - Ascend controls}
{ whether it ascending or descending}
var I,J,Count,BestCount,BestStart: integer;
var S: string;
 
function Compare(N1,N2: integer; Ascend: boolean): boolean;
{Compare for ascending or descending}
begin
if Ascend then Result:=N1>N2
else Result:=N1<N2;
end;
 
begin
BestStart:=0; BestCount:=0;
{Check all the primes in the sieve}
for I:=2 to High(Sieve.Primes)-1 do
begin
J:=I + 1;
Count:= 1;
{Count all the elements in the sequence}
while (j<=High(Sieve.Primes)) and
Compare(Sieve.Primes[J]-Sieve.Primes[J-1],Sieve.Primes[J-1]-Sieve.Primes[j-2],Ascend) do
begin
Inc(Count);
Inc(J);
end;
{Save the info if it is the best so far}
if Count > BestCount then
begin
BestStart:=I-1;
BestCount:= Count;
end;
end;
Memo.Lines.Add('Count = '+IntToStr(BestCount+1));
{Display the sequence}
S:='[';
for I:=BestStart to BestStart+BestCount do
begin
S:=S+IntToStr(Sieve.Primes[I]);
if I<(BestStart+BestCount) then S:=S+' ('+IntToStr(Sieve.Primes[I+1]-Sieve.Primes[I])+') ';
end;
S:=S+']';
Memo.Lines.Add(S);
end;
 
 
 
begin
Sieve:=TPrimeSieve.Create;
try
{Generate all primes below 1 million}
Sieve.Intialize(1000000);
 
Memo.Lines.Add('The longest sequence of ascending primes');
FindLongestSequence(True);
Memo.Lines.Add('The longest sequence of ascending primes');
FindLongestSequence(False);
finally Sieve.Free; end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
The longest sequence of ascending primes
Count = 8
[128981 (2) 128983 (4) 128987 (6) 128993 (8) 129001 (10) 129011 (12) 129023 (14) 129037]
The longest sequence of ascending primes
Count = 8
[322171 (22) 322193 (20) 322213 (16) 322229 (8) 322237 (6) 322243 (4) 322247 (2) 322249]
Elapsed Time: 18.386 ms.
 
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc nextprim num .
repeat
i = 2
while i <= sqrt num and num mod i <> 0
i += 1
.
until num mod i <> 0
num += 1
.
return num
.
proc getseq dir . maxprim maxcnt .
maxcnt = 0
pri = 2
repeat
prev = pri
pri = nextprim (pri + 1)
until pri > 1000000
d0 = d
d = (pri - prev) * dir
if d > d0
cnt += 1
else
if cnt > maxcnt
maxcnt = cnt
maxprim = prim0
.
prim0 = prev
cnt = 1
.
.
.
proc outseq pri max . .
write pri & " "
for i to max
pri = nextprim (pri + 1)
write pri & " "
.
print ""
.
getseq 1 pri max
outseq pri max
getseq -1 pri max
outseq pri max
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
Line 462 ⟶ 665:
Real: 00:00:04.708
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
Line 503 ⟶ 707:
752,207 (44) 752,251 (12) 752,263 (10) 752,273 (8) 752,281 (6) 752,287 (4) 752,291 (2) 752,293
</pre>
 
=={{header|FreeBASIC}}==
Use any of the primality testing code on this site as an include; I won't reproduce it here.
<syntaxhighlight lang="freebasic">#define UPPER 1000000
#include"isprime.bas"
 
dim as uinteger champ = 0, record = 0, streak, i, j, n
 
'first generate all the primes below UPPER
redim as uinteger prime(1 to 2)
prime(1) = 2 : prime(2) = 3
for i = 5 to UPPER step 2
if isprime(i) then
redim preserve prime(1 to ubound(prime) + 1)
prime(ubound(prime)) = i
end if
next i
n = ubound(prime)
 
'now look for the longest streak of ascending primes
for i = 2 to n-1
j = i + 1
streak = 1
while j<=n andalso prime(j)-prime(j-1) > prime(j-1)-prime(j-2)
streak += 1
j+=1
wend
if streak > record then
champ = i-1
record = streak
end if
next i
 
print "The longest sequence of ascending primes (with their difference from the last one) is:"
for i = champ+1 to champ+record
print prime(i-1);" (";prime(i)-prime(i-1);") ";
next i
print prime(i-1) : print
'now for the descending ones
 
record = 0 : champ = 0
for i = 2 to n-1
j = i + 1
streak = 1
while j<=n andalso prime(j)-prime(j-1) < prime(j-1)-prime(j-2) 'identical to above, but for the inequality sign
streak += 1
j+=1
wend
if streak > record then
champ = i-1
record = streak
end if
next i
 
print "The longest sequence of descending primes (with their difference from the last one) is:"
for i = champ+1 to champ+record
print prime(i-1);" (";prime(i)-prime(i-1);") ";
next i
print prime(i-1)</syntaxhighlight>
{{out}}
<pre>The longest sequence of ascending primes (with their difference from the last one) is:
128981 (2) 128983 (4) 128987 (6) 128993 (8) 129001 (10) 129011 (12) 129023 (14) 129037
 
The longest sequence of descending primes (with their difference from the last one) is:
322171 (22) 322193 (20) 322213 (16) 322229 (8) 322237 (6) 322243 (4) 322247 (2) 322249</pre>
 
=={{header|Go}}==
Line 681 ⟶ 820:
;{.(\: #@>) tris <@~.@,;._1~ >:/ 2 -/\ |: tris=: 3 ]\ p: i. p:inv 1e6
322171 322193 322213 322229 322237 322243 322247 322249</syntaxhighlight>
 
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
 
public final class ConsecutivePrimes {
 
public static void main(String[] aArgs) {
final int limit = 1_000_000;
List<Integer> primes = listPrimeNumbers(limit);
List<Integer> asc = new ArrayList<Integer>();
List<Integer> desc = new ArrayList<Integer>();
List<List<Integer>> maxAsc = new ArrayList<List<Integer>>();
List<List<Integer>> maxDesc = new ArrayList<List<Integer>>();
int maxAscSize = 0;
int maxDescSize = 0;
for ( int prime : primes ) {
final int ascSize = asc.size();
if ( ascSize > 1 && prime - asc.get(ascSize - 1) <= asc.get(ascSize - 1) - asc.get(ascSize - 2) ) {
asc = new ArrayList<Integer>(asc.subList(ascSize - 1, asc.size()));
}
asc.add(prime);
if ( asc.size() >= maxAscSize ) {
if ( asc.size() > maxAscSize ) {
maxAscSize = asc.size();
maxAsc.clear();
}
maxAsc.add( new ArrayList<Integer>(asc) );
}
final int descSize = desc.size();
if ( descSize > 1 && prime - desc.get(descSize - 1) >= desc.get(descSize - 1) - desc.get(descSize - 2) ) {
desc = new ArrayList<Integer>(desc.subList(descSize - 1, desc.size()));
}
desc.add(prime);
if ( desc.size() >= maxDescSize ) {
if ( desc.size() > maxDescSize ) {
maxDescSize = desc.size();
maxDesc.clear();
}
maxDesc.add( new ArrayList<Integer>(desc) );
}
}
System.out.println("Longest run(s) of ascending prime gaps up to " + limit + ":");
for ( List<Integer> list : maxAsc ) {
displayResult(list);
}
System.out.println();
System.out.println("Longest run(s) of descending prime gaps up to " + limit + ":");
for( List<Integer> list : maxDesc ) {
displayResult(list);
}
}
private static List<Integer> listPrimeNumbers(int aLimit) {
BitSet sieve = new BitSet(aLimit + 1);
sieve.set(2, aLimit + 1);
for ( int i = 2; i <= Math.sqrt(aLimit); i = sieve.nextSetBit(i + 1) ) {
for ( int j = i * i; j <= aLimit; j = j + i ) {
sieve.clear(j);
}
}
List<Integer> result = new ArrayList<Integer>(sieve.cardinality());
for ( int i = 2; i >= 0; i = sieve.nextSetBit(i + 1) ) {
result.add(i);
}
return result;
}
private static void displayResult(List<Integer> aList) {
for ( int i = 0; i < aList.size(); i++ ) {
if ( i > 0 ) {
System.out.print(" (" + ( aList.get(i) - aList.get(i - 1) ) + ") ");
}
System.out.print(aList.get(i));
}
System.out.println();
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
Longest run(s) of ascending prime gaps up to 1000000:
128981 (2) 128983 (4) 128987 (6) 128993 (8) 129001 (10) 129011 (12) 129023 (14) 129037
402581 (2) 402583 (4) 402587 (6) 402593 (8) 402601 (12) 402613 (18) 402631 (60) 402691
665111 (2) 665113 (4) 665117 (6) 665123 (8) 665131 (10) 665141 (12) 665153 (24) 665177
 
Longest run(s) of descending prime gaps up to 1000000:
322171 (22) 322193 (20) 322213 (16) 322229 (8) 322237 (6) 322243 (4) 322247 (2) 322249
752207 (44) 752251 (12) 752263 (10) 752273 (8) 752281 (6) 752287 (4) 752291 (2) 752293
</pre>
 
=={{header|jq}}==
Line 1,362 ⟶ 1,604:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var LIMIT = 999999
2,023

edits