Largest difference between adjacent primes: Difference between revisions

Content added Content deleted
(Added Sidef)
m (syntax highlighting fixup automation)
Line 7: Line 7:


=={{header|11l}}==
=={{header|11l}}==
<lang 11l>F primes_upto(limit)
<syntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 21: Line 21:
maxdiff = max(maxdiff, primes[n] - primes[n - 1])
maxdiff = max(maxdiff, primes[n] - primes[n - 1])


print(‘Largest difference is ’maxdiff)</lang>
print(‘Largest difference is ’maxdiff)</syntaxhighlight>


{{out}}
{{out}}
Line 32: Line 32:
As with the Wren, Phix, etc. samples, shows the gaps at a few other places.
As with the Wren, Phix, etc. samples, shows the gaps at a few other places.
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find the largest gap between adjacent primes up to 10 000 000 #
<syntaxhighlight lang="algol68">BEGIN # find the largest gap between adjacent primes up to 10 000 000 #
PR read "primes.incl.a68" PR
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE 10 000 000; # sieve the primes to 10 000 000 #
[]BOOL prime = PRIMESIEVE 10 000 000; # sieve the primes to 10 000 000 #
Line 64: Line 64:
FI
FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 77: Line 77:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LARGEST_DIFFERENCE_BETWEEN_ADJACENT_PRIMES.AWK
# syntax: GAWK -f LARGEST_DIFFERENCE_BETWEEN_ADJACENT_PRIMES.AWK
# converted from FreeBASIC
# converted from FreeBASIC
Line 120: Line 120:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 128: Line 128:
=={{header|C}}==
=={{header|C}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang c>#include<stdio.h>
<syntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>


Line 161: Line 161:
printf( "The largest difference was %d, between %d and %d.\n", record, champ, champj );
printf( "The largest difference was %d, between %d and %d.\n", record, champ, champj );
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}<pre>The largest difference was 114, between 492113 and 492227.</pre>
{{out}}<pre>The largest difference was 114, between 492113 and 492227.</pre>


=={{header|C++}}==
=={{header|C++}}==
{{libheader|Primesieve}}
{{libheader|Primesieve}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <locale>
#include <locale>


Line 190: Line 190:
p1 = p2;
p1 = p2;
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 208: Line 208:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Largest difference between adjacent primes. Nigel Galloway: November 22nd., 2021
// Largest difference between adjacent primes. Nigel Galloway: November 22nd., 2021
let n,g=primes32()|>Seq.takeWhile((>)1000000)|>Seq.pairwise|>Seq.maxBy(fun(n,g)->g-n) in printfn $"%d{g}-%d{n}=%d{g-n}"
let n,g=primes32()|>Seq.takeWhile((>)1000000)|>Seq.pairwise|>Seq.maxBy(fun(n,g)->g-n) in printfn $"%d{g}-%d{n}=%d{g-n}"
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 220: Line 220:
See [[Largest difference between adjacent primes/Factor]] for a detailed explanation because why not?
See [[Largest difference between adjacent primes/Factor]] for a detailed explanation because why not?
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: arrays formatting kernel lists lists.lazy math math.order
<syntaxhighlight lang="factor">USING: arrays formatting kernel lists lists.lazy math math.order
math.primes.lists sequences ;
math.primes.lists sequences ;


Line 226: Line 226:
[ second 1e6 < ] lwhile { 0 } [ max ] foldl
[ second 1e6 < ] lwhile { 0 } [ max ] foldl


"Largest difference in adjacent primes under a million: %d between %d and %d.\n" vprintf</lang>
"Largest difference in adjacent primes under a million: %d between %d and %d.\n" vprintf</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 233: Line 233:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>#include "isprime.bas"
<syntaxhighlight lang="freebasic">#include "isprime.bas"


function nextprime( n as uinteger ) as uinteger
function nextprime( n as uinteger ) as uinteger
Line 257: Line 257:
wend
wend


print using "The largest difference was ####, between ####### and #######";record;champ;champj</lang>
print using "The largest difference was ####, between ####### and #######";record;champ;champj</syntaxhighlight>
{{out}}<pre>The largest difference was 114 between 492113 and 492227</pre>
{{out}}<pre>The largest difference was 114 between 492113 and 492227</pre>


Line 263: Line 263:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|go-rcu}}
{{libheader|go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 292: Line 292:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 310: Line 310:


=={{header|GW-BASIC}}==
=={{header|GW-BASIC}}==
<lang gwbasic>10 R=2 : P=3 : P2=0
<syntaxhighlight lang="gwbasic">10 R=2 : P=3 : P2=0
20 GOSUB 190
20 GOSUB 190
30 IF P2>1000000! THEN GOTO 70
30 IF P2>1000000! THEN GOTO 70
Line 339: Line 339:
280 C2 = P2
280 C2 = P2
290 R = P2 - P
290 R = P2 - P
300 RETURN</lang>
300 RETURN</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 347: Line 347:
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.


<lang jq># Primes less than . // infinite
<syntaxhighlight lang="jq"># Primes less than . // infinite
def primes:
def primes:
(. // infinite) as $n
(. // infinite) as $n
Line 365: Line 365:


pow(10; 1, 2, 6) | largest_difference_between_adjacent_primes
pow(10; 1, 2, 6) | largest_difference_between_adjacent_primes
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 374: Line 374:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function maxprimeinterval(nmax)
function maxprimeinterval(nmax)
Line 384: Line 384:


foreach(n -> maxprimeinterval(10^n), 1:10)
foreach(n -> maxprimeinterval(10^n), 1:10)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
The maximum prime interval in primes up to 10 is 2: for example at [3, 5].
The maximum prime interval in primes up to 10 is 2: for example at [3, 5].
Line 398: Line 398:
</pre>
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>Max[-Subtract @@@
<syntaxhighlight lang="mathematica">Max[-Subtract @@@
Partition[Most@NestWhileList[NextPrime, 2, # < 1000000 &], 2, 1]]</lang>
Partition[Most@NestWhileList[NextPrime, 2, # < 1000000 &], 2, 1]]</syntaxhighlight>


{{out}}<pre>
{{out}}<pre>
Line 407: Line 407:
=={{header|Pascal}}==
=={{header|Pascal}}==
==={{header|Free Pascal}}===
==={{header|Free Pascal}}===
<lang pascal>program primesieve;
<syntaxhighlight lang="pascal">program primesieve;
// sieving small ranges of 65536 only odd numbers
// sieving small ranges of 65536 only odd numbers
//{$O+,R+}
//{$O+,R+}
Line 752: Line 752:
{$ENDIF}
{$ENDIF}
end.
end.
</syntaxhighlight>
</lang>
{{out|@TIO.RUN}}
{{out|@TIO.RUN}}
<pre>
<pre>
Line 771: Line 771:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use Primesieve qw(generate_primes);
use Primesieve qw(generate_primes);
Line 780: Line 780:
map { ($diff = $primes[$_] - $primes[$_-1]) > $max and ($max,$p) = ($diff,$_-1) } 1..$#primes;
map { ($diff = $primes[$_] - $primes[$_-1]) > $max and ($max,$p) = ($diff,$_-1) } 1..$#primes;
printf "Largest prime gap up to %d: %d - between %d and %d.\n", 10**$n, $max, @primes[$p,$p+1];
printf "Largest prime gap up to %d: %d - between %d and %d.\n", 10**$n, $max, @primes[$p,$p+1];
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Largest prime gap up to 100: 8 - between 89 and 97.
<pre>Largest prime gap up to 100: 8 - between 89 and 97.
Line 792: Line 792:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Wren}}
{{trans|Wren}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
Line 813: Line 813:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 832: Line 832:


=={{header|Python}}==
=={{header|Python}}==
<lang python>
<syntaxhighlight lang="python">
print("working...")
print("working...")
limit = 1000000
limit = 1000000
Line 862: Line 862:
print(diff)
print(diff)
print("done...")
print("done...")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 872: Line 872:
=={{header|Raku}}==
=={{header|Raku}}==
===Built-ins===
===Built-ins===
<lang perl6>for 2..8 -> $n {
<syntaxhighlight lang="raku" line>for 2..8 -> $n {
printf "Largest prime gap up to {10 ** $n}: %d - between %d and %d.\n", .[0], |.[1]
printf "Largest prime gap up to {10 ** $n}: %d - between %d and %d.\n", .[0], |.[1]
given max (^10**$n).grep(&is-prime).rotor(2=>-1).map({.[1]-.[0],$_})
given max (^10**$n).grep(&is-prime).rotor(2=>-1).map({.[1]-.[0],$_})
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Largest prime gap up to 100: 8 - between 89 and 97.
<pre>Largest prime gap up to 100: 8 - between 89 and 97.
Line 888: Line 888:
===Module===
===Module===


<lang perl6>use Math::Primesieve;
<syntaxhighlight lang="raku" line>use Math::Primesieve;
my $sieve = Math::Primesieve.new;
my $sieve = Math::Primesieve.new;


Line 894: Line 894:
printf "Largest prime gap up to {10 ** $n}: %d - between %d and %d.\n", .[0], |.[1]
printf "Largest prime gap up to {10 ** $n}: %d - between %d and %d.\n", .[0], |.[1]
given max $sieve.primes(10 ** $n).rotor(2=>-1).map({.[1]-.[0],$_})
given max $sieve.primes(10 ** $n).rotor(2=>-1).map({.[1]-.[0],$_})
}</lang>
}</syntaxhighlight>
Same output
Same output


=={{header|Ring}}==
=={{header|Ring}}==


<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"
see "working..." + nl
see "working..." + nl
Line 924: Line 924:
see nl + "Largest difference is = " + diff + nl
see nl + "Largest difference is = " + diff + nl
see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 935: Line 935:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func prime_gap_records(upto) {
<syntaxhighlight lang="ruby">func prime_gap_records(upto) {


var gaps = []
var gaps = []
Line 957: Line 957:
printf("Largest prime gap up to 10^%s is %3s between %s and %s\n",
printf("Largest prime gap up to 10^%s is %3s between %s and %s\n",
n, b.next_prime - b, b, b.next_prime)
n, b.next_prime - b, b, b.next_prime)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 972: Line 972:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 991: Line 991:
nextStop = nextStop * 10
nextStop = nextStop * 10
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,008: Line 1,008:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>def Size = 1_000_000_000/2; \sieve for odd numbers
<syntaxhighlight lang="xpl0">def Size = 1_000_000_000/2; \sieve for odd numbers
int Prime, I, K;
int Prime, I, K;
char Flags;
char Flags;
Line 1,046: Line 1,046:
Limit:= Limit*10;
Limit:= Limit*10;
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}