Ramanujan primes: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→ntheory library: note use of 'ntheory' module) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 27: | Line 27: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|Julia}} |
{{trans|Julia}} |
||
< |
<syntaxhighlight lang="cpp">#include <chrono> |
||
#include <cmath> |
#include <cmath> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 90: | Line 90: | ||
<< std::chrono::duration<double>(end - start).count() * 1000 |
<< std::chrono::duration<double>(end - start).count() * 1000 |
||
<< " milliseconds\n"; |
<< " milliseconds\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 114: | Line 114: | ||
=={{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#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Ramanujan primes. Nigel Galloway: September 7th., 2021 |
// 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 |
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: | Line 121: | ||
n.[0..99]|>Array.iter(printf "%d "); printfn "" |
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" ) |
[1000;10000;100000]|>List.iter(fun g->printf $"The %d{g}th Ramanujan prime is %d{n.[g-1]}\n" ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 135: | Line 135: | ||
<br> |
<br> |
||
This takes about 40 ms to find the 100,000th Ramanujan prime on my machine. The millionth takes about 520 ms. |
This takes about 40 ms to find the 100,000th Ramanujan prime on my machine. The millionth takes about 520 ms. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 226: | Line 226: | ||
fmt.Println("\nTook", time.Since(start)) |
fmt.Println("\nTook", time.Since(start)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 255: | Line 255: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
public class RamanujanPrimes { |
public class RamanujanPrimes { |
||
Line 313: | Line 313: | ||
private int[] count; |
private int[] count; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 337: | Line 337: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
@time let |
@time let |
||
Line 359: | Line 359: | ||
println("\nThe 10,000th Ramanujan prime is ", Ramanujan_prime(10000)) |
println("\nThe 10,000th Ramanujan prime is ", Ramanujan_prime(10000)) |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
2 11 17 29 41 47 59 67 71 97 101 107 127 149 151 167 179 181 227 229 |
2 11 17 29 41 47 59 67 71 97 101 107 127 149 151 167 179 181 227 229 |
||
Line 374: | Line 374: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">l = PrimePi[Range[10^6]] - PrimePi[Range[10^6]/2]; |
||
Multicolumn[1 + Position[l, #][[-1, 1]] & /@ Range[0, 99], {Automatic, 10}, Appearance -> "Horizontal"] |
Multicolumn[1 + Position[l, #][[-1, 1]] & /@ Range[0, 99], {Automatic, 10}, Appearance -> "Horizontal"] |
||
1 + Position[l, 999][[-1, 1]] |
1 + Position[l, 999][[-1, 1]] |
||
1 + Position[l, 9999][[-1, 1]]</ |
1 + Position[l, 9999][[-1, 1]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 11 17 29 41 47 59 67 71 97 |
<pre>2 11 17 29 41 47 59 67 71 97 |
||
Line 398: | 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). |
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). |
||
< |
<syntaxhighlight lang="nim">import math, sequtils, strutils, times |
||
let t0 = now() |
let t0 = now() |
||
Line 444: | Line 444: | ||
echo "The 100_000th Ramanujan prime is ", ramanujanPrime(pi, 100_000) |
echo "The 100_000th Ramanujan prime is ", ramanujanPrime(pi, 100_000) |
||
echo "\nElapsed time: ", (now() - t0).inMilliseconds, " ms"</ |
echo "\nElapsed time: ", (now() - t0).inMilliseconds, " ms"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 462: | Line 462: | ||
{{trans|Raku}} |
{{trans|Raku}} |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use ntheory 'primes'; |
use ntheory 'primes'; |
||
Line 488: | Line 488: | ||
print "\n\n 1000th: " . r_prime( 1000) . "\n"; |
print "\n\n 1000th: " . r_prime( 1000) . "\n"; |
||
print "\n10000th: " . r_prime(10000) . "\n"; # faster with 'ntheory' function 'ramanujan_primes'</ |
print "\n10000th: " . r_prime(10000) . "\n"; # faster with 'ntheory' function 'ramanujan_primes'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 100: |
<pre>First 100: |
||
Line 504: | Line 504: | ||
{{libheader|Phix/online}} |
{{libheader|Phix/online}} |
||
You can run this online [http://phix.x10.mx/p2js/Ramanujan_primes.htm here]. |
You can run this online [http://phix.x10.mx/p2js/Ramanujan_primes.htm here]. |
||
<!--< |
<!--<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 557: | 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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 577: | Line 577: | ||
All timings are purely informational. Will vary by system specs and load. |
All timings are purely informational. Will vary by system specs and load. |
||
=== Pure Raku === |
=== Pure Raku === |
||
<lang |
<syntaxhighlight lang="raku" line>use Math::Primesieve; |
||
use Lingua::EN::Numbers; |
use Lingua::EN::Numbers; |
||
Line 595: | Line 595: | ||
say "\n 1,000th: { comma 1000.&ramanujan-prime }"; |
say "\n 1,000th: { comma 1000.&ramanujan-prime }"; |
||
say "10,000th: { comma 10000.&ramanujan-prime }"; |
say "10,000th: { comma 10000.&ramanujan-prime }"; |
||
say (now - INIT now).fmt('%.3f') ~ ' seconds';</ |
say (now - INIT now).fmt('%.3f') ~ ' seconds';</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 100: |
<pre>First 100: |
||
Line 615: | Line 615: | ||
=== ntheory library === |
=== ntheory library === |
||
{{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 625: | Line 625: | ||
} |
} |
||
say (now - INIT now).fmt('%.3f') ~ ' seconds';</ |
say (now - INIT now).fmt('%.3f') ~ ' seconds';</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 100: |
<pre>First 100: |
||
Line 669: | Line 669: | ||
<br> |
<br> |
||
This takes about 1.1 seconds to find the 100,000th Ramanujan prime on my machine. The millionth takes 13.2 seconds. |
This takes about 1.1 seconds to find the 100,000th Ramanujan prime on my machine. The millionth takes 13.2 seconds. |
||
< |
<syntaxhighlight lang="ecmascript">import "/trait" for Stepped |
||
import "/seq" for Lst |
import "/seq" for Lst |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 724: | Line 724: | ||
Fmt.print("\nThe 100,000th Ramanujan prime is $,9d", ramanujanPrime.call(100000)) |
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))</ |
Fmt.print("\nThe 1,000,000th Ramanujan prime is $,10d", ramanujanPrime.call(1000000))</syntaxhighlight> |
||
{{out}} |
{{out}} |