Ramanujan primes/twins: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Raku}}: note use of 'ntheory' module)
m (syntax highlighting fixup automation)
Line 4:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Ramanujan_primes#F.23 Ramanujan primes (F#)]
<langsyntaxhighlight lang="fsharp">
// 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"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 16:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 121:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 135:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function rajpairs(N, verbose, interval = 2)
Line 158:
rajpairs(100000, false)
@time rajpairs(1000000, true)
</langsyntaxhighlight>{{out}}
<pre>
There are 74973 twins in the first 1000000 Ramanujan primes.
Line 165:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">$HistoryLength = 1;
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 = Take[Sort@ramanujanprimes, 10^6];
Count[Differences[ramanujanprimes], 2]</langsyntaxhighlight>
{{out}}
<pre>74973</pre>
Line 176:
{{trans|Go}}
We use Phix algorithm in its Go translation.
<langsyntaxhighlight Nimlang="nim">import math, sequtils, strutils, sugar, times
 
let t0 = now()
Line 248:
 
main()
echo "\nElapsed time: ", (now() - t0).inMilliseconds, " ms"</langsyntaxhighlight>
 
{{out}}
Line 259:
Can't do better than the <code>ntheory</code> module.
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory <ramanujan_primes nth_ramanujan_prime>;
Line 270:
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;
}</langsyntaxhighlight>
{{out}}
<pre>The 100,000th Ramanujan prime is 2,916,539
Line 283:
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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
using a smaller limit:
Line 370:
Timing is informational only. Will vary by system specs and load.
{{libheader|ntheory}}
<syntaxhighlight lang="raku" perl6line>use ntheory:from<Perl5> <ramanujan_primes nth_ramanujan_prime>;
use Lingua::EN::Numbers;
 
Line 380:
}
 
say (now - INIT now).fmt('%.3f') ~ ' seconds';</langsyntaxhighlight>
{{out}}
<pre>The 100,000th Ramanujan prime is 2,916,539
Line 396:
<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.
<langsyntaxhighlight lang="ecmascript">import "/trait" for Stepped, Indexed
import "/math" for Int, Math
import "/fmt" for Fmt
Line 464:
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")
}</langsyntaxhighlight>
 
{{out}}
10,327

edits