Anonymous user
Ramanujan primes: Difference between revisions
Replaced Phix algorithm by C++ algorithm which precomputes the values of the pi function. Added the 100_000th Ramanujan prime.
(Replaced Phix algorithm by C++ algorithm which precomputes the values of the pi function. Added the 100_000th Ramanujan prime.) |
|||
Line 326:
=={{header|Nim}}==
{{trans|
I compiled using command <code>nim c -d:release -d:lto --gc:arc ramanujan_primes.nim</code>, i.e. with runtime checks on
▲I compiled using command <code>nim c -d:release -d:lto ramanujan_primes.nim</code>, i.e. with runtime checks on and link time optimization. The program runs in about 35 ms on my laptop (i5-8250U CPU @ 1.60GHz, 8 GB Ram, Linux Manjaro). Fast, but this is normal for native code.
▲<lang Nim>import algorithm, math, strutils, times
let t0 = now()
type PrimeCounter = seq[int]
proc initPrimeCounter(limit: Positive): PrimeCounter =
doAssert limit > 1
if n2 > N: break▼
result[1] = 0
var
var p2 = 9
while p2 < limit:
if result[p] != 0:
for q in countup(p2, limit - 1, p shl 1):
p2 += (p + 1) shl 2
inc p, 2
# Compute partial sums in place.
var sum = 0
for item in result.mitems:
sum += item
item = sum
func ramanujanMax(n: int): int {.inline.} = int(ceil(4 * n.toFloat * ln(4 * n.toFloat)))
▲ if not comp: result.add i
proc ramanujanPrime(pi: PrimeCounter; n: int): int =▼
▲ result = piCache[n-1]
▲proc ramanujanPrime(n: int): int =
let maxPoss = int(ceil(4 * n.toFloat * ln(4 * n.toFloat)))
for i in countdown(maxPoss, 1):
if pi
return i + 1
let pi = initPrimeCounter(1 + ramanujanMax(100_000))
for n in 1..100:
stdout.write ($ramanujanPrime(pi, n)).align(4), if n mod 20 == 0: '\n' else: ' '
echo "\nThe 1000th Ramanujan prime is ", ramanujanPrime(
echo "The
echo "The 100_000th Ramanujan prime is ", ramanujanPrime(pi, 100_000)
echo "\nElapsed time: ", (now() - t0).inMilliseconds, " ms"</lang>
Line 383:
The 1000th Ramanujan prime is 19403
The
The 100_000th Ramanujan prime is 2916539
Elapsed time:
=={{header|Perl}}==
|