Earliest difference between prime gaps: Difference between revisions

Content added Content deleted
(J)
m (syntax highlighting fixup automation)
Line 72: Line 72:
=={{header|C++}}==
=={{header|C++}}==
{{libheader|Primesieve}}
{{libheader|Primesieve}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <locale>
#include <locale>
#include <unordered_map>
#include <unordered_map>
Line 124: Line 124:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 165: Line 165:
=={{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">
// Earliest difference between prime gaps. Nigel Galloway: December 1st., 2021
// Earliest difference between prime gaps. Nigel Galloway: December 1st., 2021
let fN y=let i=System.Collections.Generic.SortedDictionary<int64,int64>()
let fN y=let i=System.Collections.Generic.SortedDictionary<int64,int64>()
Line 172: Line 172:
[1..9]|>List.iter(fun g->let fN=fN(pown 10 g) in let n,i=(primes64()|>Seq.skip 1|>Seq.pairwise|>Seq.map fN|>Seq.find Option.isSome).Value
[1..9]|>List.iter(fun g->let fN=fN(pown 10 g) in let n,i=(primes64()|>Seq.skip 1|>Seq.pairwise|>Seq.map fN|>Seq.find Option.isSome).Value
printfn $"%10d{pown 10 g} -> distance between start of gap %d{n.Key}=%d{n.Value} and start of gap %d{i.Key}=%d{i.Value} is %d{abs((n.Value)-(i.Value))}")
printfn $"%10d{pown 10 g} -> distance between start of gap %d{n.Key}=%d{n.Value} and start of gap %d{i.Key}=%d{i.Value} is %d{abs((n.Value)-(i.Value))}")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 188: Line 188:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


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


{{out}}
{{out}}
Line 273: Line 273:
Implementation:
Implementation:


<lang J>lowpgap=: {{
<syntaxhighlight lang="j">lowpgap=: {{
magnitude=. ref=. 10^y
magnitude=. ref=. 10^y
whilst. -.1 e. ok do.
whilst. -.1 e. ok do.
Line 283: Line 283:
end.
end.
(0 1+ok i.1){mag
(0 1+ok i.1){mag
}}</lang>
}}</syntaxhighlight>


Task examples:
Task examples:


<lang J> lowpgap 1
<syntaxhighlight lang="j"> lowpgap 1
7 23
7 23
lowpgap 2
lowpgap 2
Line 300: Line 300:
396733 1444309
396733 1444309
lowpgap 7
lowpgap 7
2010733 13626257</lang>
2010733 13626257</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
Uses the PrimeGenerator class from [[Extensible prime generator#Java]].
Uses the PrimeGenerator class from [[Extensible prime generator#Java]].
<lang java>import java.util.HashMap;
<syntaxhighlight lang="java">import java.util.HashMap;
import java.util.Map;
import java.util.Map;


Line 347: Line 347:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 380: Line 380:
{{trans|Wren}}
{{trans|Wren}}
To get to 10^9 correctly we need a limit multiplier of 5 in Julia, not 4. Going up to 10^10 runs out of memory on my machine.
To get to 10^9 correctly we need a limit multiplier of 5 in Julia, not 4. Going up to 10^10 runs out of memory on my machine.
<lang julia>using Formatting
<syntaxhighlight lang="julia">using Formatting
using Primes
using Primes


Line 414: Line 414:


primegaps()
primegaps()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Earliest difference > 10 between adjacent prime gap starting primes:
Earliest difference > 10 between adjacent prime gap starting primes:
Line 444: Line 444:
</pre>
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>primes = Prime[Range[10^7]];
<syntaxhighlight lang="mathematica">primes = Prime[Range[10^7]];
gaps = {Differences[primes], Most[primes]} // Transpose;
gaps = {Differences[primes], Most[primes]} // Transpose;
tmp = GatherBy[gaps, First][[All, 1]];
tmp = GatherBy[gaps, First][[All, 1]];
Line 453: Line 453:
{10^i, n, starts[n], n + 2, starts[n + 2], k}, {i, 1, 7}];
{10^i, n, starts[n], n + 2, starts[n + 2], k}, {i, 1, 7}];
StringTemplate["Earliest difference > `` between adjacent prime gap starting primes:
StringTemplate["Earliest difference > `` between adjacent prime gap starting primes:
Gap `` starts at ``, gap `` starts at ``, difference is ``."]/*Print @@@ data;</lang>
Gap `` starts at ``, gap `` starts at ``, difference is ``."]/*Print @@@ data;</syntaxhighlight>
{{out}}
{{out}}
<pre>Earliest difference > 10 between adjacent prime gap starting primes:
<pre>Earliest difference > 10 between adjacent prime gap starting primes:
Line 472: Line 472:
=={{header|Pascal}}==
=={{header|Pascal}}==
==={{header|Free Pascal}}===
==={{header|Free Pascal}}===
<lang pascal>program primesieve;
<syntaxhighlight lang="pascal">program primesieve;
// sieving small ranges of 65536
// sieving small ranges of 65536
//{$O+,R+}
//{$O+,R+}
Line 869: Line 869:
writeln('Press <Enter>');readln;
writeln('Press <Enter>');readln;
{$ENDIF}
{$ENDIF}
end.</lang>
end.</syntaxhighlight>
{{out|@TIO.RUN}}
{{out|@TIO.RUN}}
<pre>
<pre>
Line 902: Line 902:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Earliest_difference_between_prime_gaps
use strict; # https://rosettacode.org/wiki/Earliest_difference_between_prime_gaps
Line 925: Line 925:
print "above $m gap @{[$i * 2 - 2 ]} abs( $gaps[$i-1] - $gaps[$i] )\n";
print "above $m gap @{[$i * 2 - 2 ]} abs( $gaps[$i-1] - $gaps[$i] )\n";
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 940: Line 940:
=={{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: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1e7</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1e8</span><span style="color: #0000FF;">),</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1e7</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1e8</span><span style="color: #0000FF;">),</span>
Line 974: Line 974:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
Output same as Wren. Takes 5s on desktop/Phix but would take 17s under p2js so limited that to keep it under 2s.
Output same as Wren. Takes 5s on desktop/Phix but would take 17s under p2js so limited that to keep it under 2s.
A limit of 1e9 needs 64 bit (hence not p2js compatible), and a multiplier of 5, and a gslim of 354, and takes 2 mins 43s, and nearly killed my poor little box, but matched the output of Julia, which managed it in 40s (and with no signs of any stress). Python needed more memory than I've got for 1e9, but took 15s for a limit of 1e8, while Wren (bless, also 1e8) took just over 3 minutes (an old i5/8GB/w10).
A limit of 1e9 needs 64 bit (hence not p2js compatible), and a multiplier of 5, and a gslim of 354, and takes 2 mins 43s, and nearly killed my poor little box, but matched the output of Julia, which managed it in 40s (and with no signs of any stress). Python needed more memory than I've got for 1e9, but took 15s for a limit of 1e8, while Wren (bless, also 1e8) took just over 3 minutes (an old i5/8GB/w10).
Line 980: Line 980:
=={{header|Python}}==
=={{header|Python}}==
{{trans|Julia, Wren}}
{{trans|Julia, Wren}}
<lang python>""" https://rosettacode.org/wiki/Earliest_difference_between_prime_gaps """
<syntaxhighlight lang="python">""" https://rosettacode.org/wiki/Earliest_difference_between_prime_gaps """


from primesieve import primes
from primesieve import primes
Line 1,010: Line 1,010:
else:
else:
GAP1 = GAP2
GAP1 = GAP2
</lang>{{out}}Same as Raku, Wren and Julia versions.
</syntaxhighlight>{{out}}Same as Raku, Wren and Julia versions.


=={{header|Raku}}==
=={{header|Raku}}==


1e1 through 1e7 are pretty speedy (less than 5 seconds total). 1e8 and 1e9 take several minutes.
1e1 through 1e7 are pretty speedy (less than 5 seconds total). 1e8 and 1e9 take several minutes.
<lang perl6>use Math::Primesieve;
<syntaxhighlight lang="raku" line>use Math::Primesieve;
use Lingua::EN::Numbers;
use Lingua::EN::Numbers;


Line 1,045: Line 1,045:
|(2 * $key + 2, @upto[$key], 2 * $key + 4, @upto[$key+1], abs(@upto[$key] - @upto[$key+1]))».&comma;
|(2 * $key + 2, @upto[$key], 2 * $key + 4, @upto[$key+1], abs(@upto[$key] - @upto[$key+1]))».&comma;
True
True
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Earliest difference > 10 between adjacent prime gap starting primes:
<pre>Earliest difference > 10 between adjacent prime gap starting primes:
Line 1,075: Line 1,075:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>// [dependencies]
<syntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
// primal = "0.3"


Line 1,129: Line 1,129:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,169: Line 1,169:


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


var gaps = []
var gaps = []
Line 1,192: Line 1,192:
}
}
})
})
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,208: Line 1,208:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
This uses a segmented sieve to avoid running out of memory when looking for the earliest difference above 1 billion. Takes a little over 5½ minutes to run (25 seconds to reach first 100 million) on my machine (core i7, 32GB RAM, Ubuntu 20.04).
This uses a segmented sieve to avoid running out of memory when looking for the earliest difference above 1 billion. Takes a little over 5½ minutes to run (25 seconds to reach first 100 million) on my machine (core i7, 32GB RAM, Ubuntu 20.04).
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "/fmt" for Fmt
import "/fmt" for Fmt
Line 1,238: Line 1,238:
gap1 = gap2
gap1 = gap2
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}