Ramanujan primes: Difference between revisions

Added Go
m (→‎{{header|Wren}}: Corrected libheader)
(Added Go)
Line 24:
:*   The Wikipedia entry: [https://en.wikipedia.org/wiki/Ramanujan_prime Ramanujan_prime].
 
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
A decent time though not as quick as Phix.
<lang go>package main
 
import (
"fmt"
"math"
"rcu"
"sort"
"time"
)
 
var start = time.Now()
 
var primes = rcu.Primes(700000) // say
 
func ramanujan(n int) int {
fn := float64(n)
max := int(4 * fn * math.Log(4*fn) / math.Ln2)
pi := sort.SearchInts(primes[2*n:], max) // binary search from min of (2n)th prime
for {
if pi+1-rcu.PrimeCount(primes[pi]/2) <= n {
return primes[pi]
}
pi--
}
return 0
}
 
func main() {
fmt.Println("The first 100 Ramanujan primes are:")
rams := make([]int, 100)
for n := 0; n < 100; n++ {
rams[n] = ramanujan(n + 1)
}
for i, r := range rams {
fmt.Printf("%5s ", rcu.Commatize(r))
if (i+1)%10 == 0 {
fmt.Println()
}
}
 
fmt.Printf("\nThe 1,000th Ramanujan prime is %6s\n", rcu.Commatize(ramanujan(1000)))
 
fmt.Printf("\nThe 10,000th Ramanujan primes is %7s\n", rcu.Commatize(ramanujan(10000)))
 
fmt.Println("\nTook", time.Since(start))
}</lang>
 
{{out}}
<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
 
The 10,000th Ramanujan primes is 242,057
 
Took 946.193311ms
</pre>
 
=={{header|Julia}}==
9,476

edits