Ramanujan primes: Difference between revisions

Added FreeBASIC and Yabasic
(Added FreeBASIC and Yabasic)
Line 83:
The 10000th Ramanujan prime is: 242057
</pre>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
{{trans|Phix}}
<syntaxhighlight lang="vbnet">#define floor(x) ((x*2.0-0.5) Shr 1)
 
Dim Shared As Integer pi()
 
Sub primeCounter(limit As Integer)
Dim As Integer i, q, p, sq, total
Redim pi(limit)
pi(0) = 0
pi(1) = 0
For i = 2 To limit
pi(i) = 1
Next
If limit > 2 Then
For i = 4 To limit Step 2
pi(i) = 0
Next i
p = 3
sq = 9
While sq <= limit
If pi(p) <> 0 Then
For q = sq To limit Step p*2
pi(q) = 0
Next q
End If
sq += (p + 1) * 4
p += 2
Wend
total = 0
For i = 2 To limit
total += pi(i)
pi(i) = total
Next i
End If
End Sub
 
Function ramanujanMax(n As Integer) As Integer
Return floor(4 * n * Log(4*n))
End Function
 
Function ramanujanPrime(n As Integer) As Integer
Dim As Integer i, maxposs
If n = 1 Then Return 2
maxposs = ramanujanMax(n)
For i = maxposs - (maxposs Mod 2) To 1 Step -2
If pi(i) - pi(i\2) < n Then Return i + 1
Next i
Return 0
End Function
 
Dim As Integer p, n, limit = 1e6
Dim As Double t0 = Timer
primeCounter(ramanujanMax(limit))
Print "The first 100 Ramanujan primes are:"
For p = 1 To 100
Print Using " ####"; ramanujanPrime(p);
If p Mod 20 = 0 Then Print
Next p
Print
For p = 3 To 6
n = 10 ^ p
Print Using "The &th Ramanujan prime is &"; n; ramanujanPrime(n)
Next p
Print Using "##.##sec."; Timer - t0
 
Sleep</syntaxhighlight>
{{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 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
The 1000000th Ramanujan prime is 34072993
1.53sec.</pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">dim cnt(1e6)
 
primeCounter(ramanujanMax((1e6)))
print "The first 100 Ramanujan primes are:"
for p = 1 to 100
print ramanujanPrime(p) using ("#####");
if mod(p, 20) = 0 print
next p
print
for p = 3 to 6
n = 10 ^ p
print "The ", n, "th Ramanujan prime is ", ramanujanPrime(n)
next p
print peek("millisrunning") / 1000, "sec."
end
 
sub primeCounter(limit)
local i, q, p, sq, total
redim cnt(limit)
cnt(0) = 0
cnt(1) = 0
for i = 2 to limit
cnt(i) = 1
next
if limit > 2 then
for i = 4 to limit step 2
cnt(i) = 0
next i
p = 3
sq = 9
while sq <= limit
if cnt(p) <> 0 then
for q = sq to limit step p*2
cnt(q) = 0
next q
fi
sq = sq + (p + 1) * 4
p = p + 2
wend
total = 0
for i = 2 to limit
total = total + cnt(i)
cnt(i) = total
next i
fi
end sub
 
sub ramanujanMax(n)
return floor(4 * n * log(4*n))
end sub
 
sub ramanujanPrime(n)
local i, maxposs
if n = 1 return 2
maxposs = ramanujanMax(n)
for i = maxposs - mod(maxposs, 2) to 1 step -2
if cnt(i) - cnt(floor(i/2)) < n return i + 1
next i
return 0
end sub</syntaxhighlight>
{{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 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
The 1000000th Ramanujan prime is 34072993
35.14sec.</pre>
 
=={{header|C++}}==
2,130

edits