Largest difference between adjacent primes: Difference between revisions
Largest difference between adjacent primes (view source)
Revision as of 17:28, 27 August 2022
, 1 year agosyntax highlighting fixup automation
(Added Sidef) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7:
=={{header|11l}}==
<
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 21:
maxdiff = max(maxdiff, primes[n] - primes[n - 1])
print(‘Largest difference is ’maxdiff)</
{{out}}
Line 32:
As with the Wren, Phix, etc. samples, shows the gaps at a few other places.
{{libheader|ALGOL 68-primes}}
<
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE 10 000 000; # sieve the primes to 10 000 000 #
Line 64:
FI
OD
END</
{{out}}
<pre>
Line 77:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f LARGEST_DIFFERENCE_BETWEEN_ADJACENT_PRIMES.AWK
# converted from FreeBASIC
Line 120:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 128:
=={{header|C}}==
{{trans|FreeBASIC}}
<
#include<stdlib.h>
Line 161:
printf( "The largest difference was %d, between %d and %d.\n", record, champ, champj );
return 0;
}</
{{out}}<pre>The largest difference was 114, between 492113 and 492227.</pre>
=={{header|C++}}==
{{libheader|Primesieve}}
<
#include <locale>
Line 190:
p1 = p2;
}
}</
{{out}}
Line 208:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// 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}"
</syntaxhighlight>
{{out}}
<pre>
Line 220:
See [[Largest difference between adjacent primes/Factor]] for a detailed explanation because why not?
{{works with|Factor|0.99 2021-06-02}}
<
math.primes.lists sequences ;
Line 226:
[ second 1e6 < ] lwhile { 0 } [ max ] foldl
"Largest difference in adjacent primes under a million: %d between %d and %d.\n" vprintf</
{{out}}
<pre>
Line 233:
=={{header|FreeBASIC}}==
<
function nextprime( n as uinteger ) as uinteger
Line 257:
wend
print using "The largest difference was ####, between ####### and #######";record;champ;champj</
{{out}}<pre>The largest difference was 114 between 492113 and 492227</pre>
Line 263:
{{trans|Wren}}
{{libheader|go-rcu}}
<
import (
Line 292:
}
}
}</
{{out}}
Line 310:
=={{header|GW-BASIC}}==
<
20 GOSUB 190
30 IF P2>1000000! THEN GOTO 70
Line 339:
280 C2 = P2
290 R = P2 - P
300 RETURN</
=={{header|jq}}==
Line 347:
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.
<
def primes:
(. // infinite) as $n
Line 365:
pow(10; 1, 2, 6) | largest_difference_between_adjacent_primes
</syntaxhighlight>
{{out}}
<pre>
Line 374:
=={{header|Julia}}==
<
function maxprimeinterval(nmax)
Line 384:
foreach(n -> maxprimeinterval(10^n), 1:10)
</
<pre>
The maximum prime interval in primes up to 10 is 2: for example at [3, 5].
Line 398:
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Partition[Most@NestWhileList[NextPrime, 2, # < 1000000 &], 2, 1]]</
{{out}}<pre>
Line 407:
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<
// sieving small ranges of 65536 only odd numbers
//{$O+,R+}
Line 752:
{$ENDIF}
end.
</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
Line 771:
=={{header|Perl}}==
<
use warnings;
use Primesieve qw(generate_primes);
Line 780:
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];
}</
{{out}}
<pre>Largest prime gap up to 100: 8 - between 89 and 97.
Line 792:
=={{header|Phix}}==
{{trans|Wren}}
<!--<
<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>
Line 813:
<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>
<!--</
{{out}}
<pre>
Line 832:
=={{header|Python}}==
<
print("working...")
limit = 1000000
Line 862:
print(diff)
print("done...")
</syntaxhighlight>
{{out}}
<pre>
Line 872:
=={{header|Raku}}==
===Built-ins===
<syntaxhighlight lang="raku"
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],$_})
}</
{{out}}
<pre>Largest prime gap up to 100: 8 - between 89 and 97.
Line 888:
===Module===
<syntaxhighlight lang="raku"
my $sieve = Math::Primesieve.new;
Line 894:
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],$_})
}</
Same output
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 924:
see nl + "Largest difference is = " + diff + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 935:
=={{header|Sidef}}==
<
var gaps = []
Line 957:
printf("Largest prime gap up to 10^%s is %3s between %s and %s\n",
n, b.next_prime - b, b, b.next_prime)
}</
{{out}}
<pre>
Line 972:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 991:
nextStop = nextStop * 10
}
}</
{{out}}
Line 1,008:
=={{header|XPL0}}==
<
int Prime, I, K;
char Flags;
Line 1,046:
Limit:= Limit*10;
];
]</
{{out}}
|