Ramanujan primes: Difference between revisions

→‎{{header|Wren}}: Various changes, about 3 times faster than before.
(→‎{{header|Phix}}: missed the obvious optimisation)
(→‎{{header|Wren}}: Various changes, about 3 times faster than before.)
Line 119:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
Stretch goal takes a while - just over 62 minutes on my machine compared to Julia's 36 seconds - but that's not too bad for the Wren interpreter.
<lang ecmascript>import "/math" for Int
import "/trait" for Indexed
import "/seq" for Lst
import "/fmt" for Fmt
import "/traitsort" for IndexedFind
 
var primes = Int.primeSieve(1e6700000) // say
 
var ramanujan = Fn.new { |n|
var imax = (4 * n * (4 * n).log / 2.log).floor
var pi = 0Find.all(primes, max)[2].from // binary search
for (se in Indexed.new(primes)) {
if (se.value > i) {
pi = se.index - 1
break
}
}
while (true) {
var delta = Int.primeCount(primes[pi]) + 1 - Int.primeCount((primes[pi]/2).floor)
if (delta <= n) return primes[pi]
pi = pi - 1
Line 154 ⟶ 148:
<pre>
The first 100 Ramanujan primes are:
2 11 17 29 41 47 59 67 71 97
101 107 127 149 151 167 179 181 227 229
233 239 241 263 269 281 307 311 347 349
367 373 401 409 419 431 433 439 461 487
491 503 569 571 587 593 599 601 607 641
643 647 653 659 677 719 727 739 751 769
809 821 823 827 853 857 881 937 941 947
967 983 1,009 1,019 1,021 1,031 1,049 1,051 1,061 1,063
1,087 1,091 1,097 1,103 1,151 1,163 1,187 1,217 1,229 1,249
1,277 1,289 1,297 1,301 1,367 1,373 1,423 1,427 1,429 1,439
 
The 1,000th Ramanujan prime is 19,403
9,485

edits