Ramanujan primes: Difference between revisions

Added completed solution in python
(Added FreeBASIC and Yabasic)
(Added completed solution in python)
 
Line 1,044:
</pre>
<small>The last line is omitted under pwa/p2js since the primeCounter array is too much for Javascript to handle.</small>
 
=={{header|Python}}==
{{trans|Java}}
<syntaxhighlight lang="python">
import time
import math
 
 
class PrimeCounter:
"""Generate a list 'count' where count[n] is the number of primes less than or equal to n"""
def __init__(self, limit):
self.count = [1] * limit
count = self.count
if limit > 0:
count[0] = 0
if limit > 1:
count[1] = 0
for i in range(4, limit, 2):
count[i] = 0
p = 3
while p**2 < limit:
if count[p] != 0:
q = p**2
while q < limit:
count[q] = 0
q += p * 2
p += 2
for i, j in enumerate(count):
if i == 0:
continue
count[i] += count[i - 1]
 
def prime_count(self, n):
"""Get the number of primes less than or equal to n"""
if n > 0:
return self.count[n]
return 0
 
 
def ramanujan_prime_upper_bound(n):
"""Calculate the largest number the nth Ramanujan number could possibly be"""
return math.ceil(4 * n * math.log(4 * n))
 
 
def ramanujan_prime(prime_counter_in, n):
"""Generate the nth Ramanujan prime by finding the largest number 'i' where less than n primes are in the
range i//2 and i - the Ramanujan prime is one more than i"""
pci = prime_counter_in
 
for i in range(ramanujan_prime_upper_bound(n), -1, -1):
pi_n = pci.prime_count(i)
pi_half_n = pci.prime_count(i // 2)
 
if pi_n - pi_half_n < n:
return i + 1
return 0
 
 
def print_ramanujan_primes_in_range(start_number, end_number, prime_counter_in):
"""Print all Ramanujan primes between start_number (inclusive) and end_number (exclusive)"""
for i in range(start_number, end_number):
p = ramanujan_prime(prime_counter_in, i)
print(f"{p : <4}", end=" ")
if i % 10 == 0:
print()
 
 
def solve():
"""Return the first 100 Ramanujan primes and the 1000th, 10000th & 100000th Ramanujan primes"""
print("First 100 Ramanujan primes:\n")
 
largest_number_to_calculate = ramanujan_prime_upper_bound(100000) + 1
prime_counter = PrimeCounter(largest_number_to_calculate)
 
print_ramanujan_primes_in_range(1, 101, prime_counter)
print()
 
for number in (1_000, 10_000, 100_000):
answer = ramanujan_prime(prime_counter, number)
print(
f"The {number : >6}th ramanujan prime is {answer : >7}"
)
 
 
start = time.perf_counter()
solve()
end = time.perf_counter()
time_taken_ms = int((end - start) * 1000)
print(f"\nElapsed time: {time_taken_ms}ms")</syntaxhighlight>
{{out}}
<pre>
First 100 Ramanujan primes:
 
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 1009 1019 1021 1031 1049 1051 1061 1063
1087 1091 1097 1103 1151 1163 1187 1217 1229 1249
1277 1289 1297 1301 1367 1373 1423 1427 1429 1439
 
The 1000th ramanujan prime is 19403
The 10000th ramanujan prime is 242057
The 100000th ramanujan prime is 2916539
 
Elapsed time: 1460ms
</pre>
 
=={{header|Raku}}==
2

edits