Ramanujan primes/twins: Difference between revisions

Added FreeBASIC
(Add Scala implementation)
(Added FreeBASIC)
 
Line 185:
There are 74973 twins in the first million Ramanujan primes
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">#define floor(x) ((x*2.0-0.5) Shr 1)
 
Dim Shared pi() As Integer
 
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 ramanujanPrimeTwins(n As Integer) As Integer
Dim As Integer maxposs, p1, p, cnt
maxposs = ramanujanMax(n)
maxposs -= maxposs Mod 2
While maxposs >= 2
If pi(maxposs) - pi(maxposs \ 2) < n Then
p1 = p
p = maxposs + 1
If p1 - p = 2 Then cnt += 1
n -= 1
End If
maxposs -= 2
Wend
Return cnt
End Function
 
Dim As Integer limit = 1e6
Dim As Double t0 = Timer
primeCounter ramanujanMax(limit)
Print Using "There are & twins in the first & Ramanujan primes"; ramanujanPrimeTwins(limit); limit
Print Using "##.##sec."; Timer - t0
 
Sleep</syntaxhighlight>
{{out}}
<pre>There are 74973 twins in the first 1000000 Ramanujan primes
1.53sec.</pre>
 
=={{header|Go}}==
2,123

edits