Ramanujan primes: Difference between revisions

m
syntax highlighting fixup automation
m (→‎ntheory library: note use of 'ntheory' module)
m (syntax highlighting fixup automation)
Line 27:
=={{header|C++}}==
{{trans|Julia}}
<langsyntaxhighlight lang="cpp">#include <chrono>
#include <cmath>
#include <iomanip>
Line 90:
<< std::chrono::duration<double>(end - start).count() * 1000
<< " milliseconds\n";
}</langsyntaxhighlight>
 
{{out}}
Line 114:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Ramanujan primes. Nigel Galloway: September 7th., 2021
let fN g=if isPrime g then 1 else if g%2=1 then 0 else if isPrime(g/2) then -1 else 0
Line 121:
n.[0..99]|>Array.iter(printf "%d "); printfn ""
[1000;10000;100000]|>List.iter(fun g->printf $"The %d{g}th Ramanujan prime is %d{n.[g-1]}\n" )
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 135:
<br>
This takes about 40 ms to find the 100,000th Ramanujan prime on my machine. The millionth takes about 520 ms.
<langsyntaxhighlight lang="go">package main
 
import (
Line 226:
 
fmt.Println("\nTook", time.Since(start))
}</langsyntaxhighlight>
 
{{out}}
Line 255:
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class RamanujanPrimes {
Line 313:
private int[] count;
}
}</langsyntaxhighlight>
 
{{out}}
Line 337:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
@time let
Line 359:
println("\nThe 10,000th Ramanujan prime is ", Ramanujan_prime(10000))
end
</langsyntaxhighlight>{{out}}
<pre>
2 11 17 29 41 47 59 67 71 97 101 107 127 149 151 167 179 181 227 229
Line 374:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">l = PrimePi[Range[10^6]] - PrimePi[Range[10^6]/2];
Multicolumn[1 + Position[l, #][[-1, 1]] & /@ Range[0, 99], {Automatic, 10}, Appearance -> "Horizontal"]
1 + Position[l, 999][[-1, 1]]
1 + Position[l, 9999][[-1, 1]]</langsyntaxhighlight>
{{out}}
<pre>2 11 17 29 41 47 59 67 71 97
Line 398:
I compiled using command <code>nim c -d:release -d:lto --gc:arc ramanujan_primes.nim</code>, i.e. with runtime checks on, link time optimization and using Arc garbage collector. To find the 100_000th Ramanujan prime, the program runs in about 100 ms on my laptop (i5-8250U CPU @ 1.60GHz, 8 GB Ram, Linux Manjaro).
 
<langsyntaxhighlight Nimlang="nim">import math, sequtils, strutils, times
 
let t0 = now()
Line 444:
echo "The 100_000th Ramanujan prime is ", ramanujanPrime(pi, 100_000)
 
echo "\nElapsed time: ", (now() - t0).inMilliseconds, " ms"</langsyntaxhighlight>
 
{{out}}
Line 462:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'primes';
Line 488:
 
print "\n\n 1000th: " . r_prime( 1000) . "\n";
print "\n10000th: " . r_prime(10000) . "\n"; # faster with 'ntheory' function 'ramanujan_primes'</langsyntaxhighlight>
{{out}}
<pre>First 100:
Line 504:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Ramanujan_primes.htm here].
<!--<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 557:
<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;">"The %,dth Ramanujan prime is %,d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ramanujanPrime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 577:
All timings are purely informational. Will vary by system specs and load.
=== Pure Raku ===
<syntaxhighlight lang="raku" perl6line>use Math::Primesieve;
use Lingua::EN::Numbers;
 
Line 595:
say "\n 1,000th: { comma 1000.&ramanujan-prime }";
say "10,000th: { comma 10000.&ramanujan-prime }";
say (now - INIT now).fmt('%.3f') ~ ' seconds';</langsyntaxhighlight>
{{out}}
<pre>First 100:
Line 615:
=== ntheory library ===
{{libheader|ntheory}}
<syntaxhighlight lang="raku" perl6line>use ntheory:from<Perl5> <ramanujan_primes nth_ramanujan_prime>;
use Lingua::EN::Numbers;
 
Line 625:
}
 
say (now - INIT now).fmt('%.3f') ~ ' seconds';</langsyntaxhighlight>
{{out}}
<pre>First 100:
Line 669:
<br>
This takes about 1.1 seconds to find the 100,000th Ramanujan prime on my machine. The millionth takes 13.2 seconds.
<langsyntaxhighlight lang="ecmascript">import "/trait" for Stepped
import "/seq" for Lst
import "/fmt" for Fmt
Line 724:
Fmt.print("\nThe 100,000th Ramanujan prime is $,9d", ramanujanPrime.call(100000))
 
Fmt.print("\nThe 1,000,000th Ramanujan prime is $,10d", ramanujanPrime.call(1000000))</langsyntaxhighlight>
 
{{out}}
10,327

edits