Ramanujan primes/twins: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: note use of 'ntheory' module) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 4: | Line 4: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Ramanujan_primes#F.23 Ramanujan primes (F#)] |
This task uses [http://www.rosettacode.org/wiki/Ramanujan_primes#F.23 Ramanujan primes (F#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Twin Ramanujan primes. Nigel Galloway: September 9th., 2021 |
// Twin Ramanujan primes. Nigel Galloway: September 9th., 2021 |
||
printfn $"There are %d{rP 1000000|>Seq.pairwise|>Seq.filter(fun(n,g)->n=g-2)|>Seq.length} twins in the first million Ramanujan primes" |
printfn $"There are %d{rP 1000000|>Seq.pairwise|>Seq.filter(fun(n,g)->n=g-2)|>Seq.length} twins in the first million Ramanujan primes" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 16: | Line 16: | ||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 121: | Line 121: | ||
fmt.Println() |
fmt.Println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 135: | Line 135: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
function rajpairs(N, verbose, interval = 2) |
function rajpairs(N, verbose, interval = 2) |
||
Line 158: | Line 158: | ||
rajpairs(100000, false) |
rajpairs(100000, false) |
||
@time rajpairs(1000000, true) |
@time rajpairs(1000000, true) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
There are 74973 twins in the first 1000000 Ramanujan primes. |
There are 74973 twins in the first 1000000 Ramanujan primes. |
||
Line 165: | Line 165: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">$HistoryLength = 1; |
||
l = PrimePi[Range[35 10^6]] - PrimePi[Range[35 10^6]/2]; |
l = PrimePi[Range[35 10^6]] - PrimePi[Range[35 10^6]/2]; |
||
ramanujanprimes = GatherBy[Transpose[{Range[2, Length[l] + 1], l}], Last][[All, -1, 1]]; |
ramanujanprimes = GatherBy[Transpose[{Range[2, Length[l] + 1], l}], Last][[All, -1, 1]]; |
||
ramanujanprimes = Take[Sort@ramanujanprimes, 10^6]; |
ramanujanprimes = Take[Sort@ramanujanprimes, 10^6]; |
||
Count[Differences[ramanujanprimes], 2]</ |
Count[Differences[ramanujanprimes], 2]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>74973</pre> |
<pre>74973</pre> |
||
Line 176: | Line 176: | ||
{{trans|Go}} |
{{trans|Go}} |
||
We use Phix algorithm in its Go translation. |
We use Phix algorithm in its Go translation. |
||
< |
<syntaxhighlight lang="nim">import math, sequtils, strutils, sugar, times |
||
let t0 = now() |
let t0 = now() |
||
Line 248: | Line 248: | ||
main() |
main() |
||
echo "\nElapsed time: ", (now() - t0).inMilliseconds, " ms"</ |
echo "\nElapsed time: ", (now() - t0).inMilliseconds, " ms"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 259: | Line 259: | ||
Can't do better than the <code>ntheory</code> module. |
Can't do better than the <code>ntheory</code> module. |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use ntheory <ramanujan_primes nth_ramanujan_prime>; |
use ntheory <ramanujan_primes nth_ramanujan_prime>; |
||
Line 270: | Line 270: | ||
printf "There are %s twins in the first %s Ramanujan primes.\n\n", |
printf "There are %s twins in the first %s Ramanujan primes.\n\n", |
||
comma( scalar grep { $rp->[$_+1] == $rp->[$_]+2 } 0 .. $limit-2 ), comma $limit; |
comma( scalar grep { $rp->[$_+1] == $rp->[$_]+2 } 0 .. $limit-2 ), comma $limit; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The 100,000th Ramanujan prime is 2,916,539 |
<pre>The 100,000th Ramanujan prime is 2,916,539 |
||
Line 283: | Line 283: | ||
Calculating pi(p) - pi(floor(pi/2) for all (normal) primes below that one millionth, and then |
Calculating pi(p) - pi(floor(pi/2) for all (normal) primes below that one millionth, and then |
||
filtering them based on that list is significantly faster, and finally we can scan for twins. |
filtering them based on that list is significantly faster, and finally we can scan for twins. |
||
<!--< |
<!--<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;">sequence</span> <span style="color: #000000;">pi</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">pi</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
||
Line 352: | Line 352: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %,d twins in the first %,d Ramanujan primes\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">twins</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %,d twins in the first %,d Ramanujan primes\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">twins</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
using a smaller limit: |
using a smaller limit: |
||
Line 370: | Line 370: | ||
Timing is informational only. Will vary by system specs and load. |
Timing is informational only. Will vary by system specs and load. |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
<lang |
<syntaxhighlight lang="raku" line>use ntheory:from<Perl5> <ramanujan_primes nth_ramanujan_prime>; |
||
use Lingua::EN::Numbers; |
use Lingua::EN::Numbers; |
||
Line 380: | Line 380: | ||
} |
} |
||
say (now - INIT now).fmt('%.3f') ~ ' seconds';</ |
say (now - INIT now).fmt('%.3f') ~ ' seconds';</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The 100,000th Ramanujan prime is 2,916,539 |
<pre>The 100,000th Ramanujan prime is 2,916,539 |
||
Line 396: | Line 396: | ||
<br> |
<br> |
||
As calculating the first 1 million Ramanujan primes individually is out of the question, we follow the Phix entry's clever strategy here which brings this task comfortably within our ambit. |
As calculating the first 1 million Ramanujan primes individually is out of the question, we follow the Phix entry's clever strategy here which brings this task comfortably within our ambit. |
||
< |
<syntaxhighlight lang="ecmascript">import "/trait" for Stepped, Indexed |
||
import "/math" for Int, Math |
import "/math" for Int, Math |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 464: | Line 464: | ||
Fmt.print("There are $,d twins in the first $,d Ramanujan primes.", twins, limit) |
Fmt.print("There are $,d twins in the first $,d Ramanujan primes.", twins, limit) |
||
System.print("Took %(Math.toPlaces(System.clock -start, 2, 0)) seconds.\n") |
System.print("Took %(Math.toPlaces(System.clock -start, 2, 0)) seconds.\n") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |