Ramanujan primes: Difference between revisions

m
→‎Pure Raku: Revert to slower pure Raku version
m (→‎Pure Raku: Revert to slower pure Raku version)
Line 559:
All timings are purely informational. Will vary by system specs and load.
=== Pure Raku ===
<lang perl6>use LinguaMath::EN::NumbersPrimesieve;
use Lingua::EN::Numbers;
 
my $primes = Math::Primesieve.new;
my @pi-primes = lazy flat 0, (1..*).map: { state $pi = 0; $pi += .is-prime };
my @pi-diff = lazy flat 0, (1..*).map: { @pi-primes[$_] - @pi-primes[$_ div 2] };
 
my @mem;
my @ramanujan-primes = lazy flat 2, (1..*).map: &is-ramanujan-prime;
 
sub is-ramanujan-prime ($\n) {
1 + (1..(4×n × (4×n).log / 2.log).floor).first: :end, -> \x {
state $start = 0;
my \y = x div 2;
$start += ($start..*).first: { @pi-diff[$_] > ($n+1) }, :k;
1 + (1..(@mem[x] //= $start)primes.first:count(x)) :end, {- (@pi-diffmem[$_y] <//= $nprimes.count(y))) }< n
}
}
 
say 'First 100:';
say @(1..100).map( &ramanujan-primes[^100]prime ).batch(10)».&comma».fmt("%6s").join: "\n";
say "\n 1,000th: { comma @1000.&ramanujan-primes[999]prime }";
say "10,000th: { comma @10000.&ramanujan-primes[9999]prime }";
 
say (now - INIT now).fmt('%.3f') ~ ' seconds';</lang>
{{out}}
<pre>First 100:
2 11 17 29 41 47 59 67 71 97
101 107 109127 137149 151 167 179 181 191227 197229
233 239 241 263 269 281 283307 311 347 349
367 373 401 409 419 431 433 439 461 487
491 503 521569 571 587 593 599 601 607 617641
643 647 653 659 677 683719 727 739 751 769
809 821 823 827 853 857 881 907937 941 947
967 983 9911,009 1,019 1,021 1,031 1,033049 1,051 1,061 1,063
1,087 1,091 1,097 1,103 1,117151 1,163 1,187 1,217 1,229 1,249
1,277 1,289 1,297 1,301 1,319367 1,373 1,423 1,427 1,429 1,439
 
1,000th: 19,403
10,000th: 241242,963057
318.119405 seconds</pre>
 
=== ntheory library ===
10,333

edits