RosettaCodeData/Task/Prime-conspiracy/FreeBASIC/prime-conspiracy.basic

58 lines
1.5 KiB
Plaintext

' version 13-04-2017
' updated 09-08-2018 Using bit-sieve of odd numbers
' compile with: fbc -s console
' compile with: fbc -s console -Wc -O2 ->more than 2x faster(20.2-> 8,7s)
const max = 2040*1000*1000 ' enough for 100,000,000 primes
const max2 = (max -1) \ 2
Dim As uByte _bit(7)
Dim shared As uByte sieve(max2 \ 8 + 1)
Dim shared As ULong end_digit(1 To 9, 1 To 9)
Dim As ULong i, j, x, i1, j1, x1, c, c1
Dim As String frmt_str = " # " + Chr(26) + " # count:######## frequency:##.##%"
' bit Mask
For i = 0 To 7
_bit(i) = 1 shl i
Next
' sieving
For i = 1 To (sqr(max) -1) / 2
x = 2*i+1
If (sieve(i Shr 3) And _bit(i And 7)) = 0 Then
For j = (2*i+2)*i To max2 Step x
sieve(j Shr 3) or= _bit(j And 7)
Next
End If
Next
' count
x = 2 : c = 1
For i = 1 To max2
If (sieve(i Shr 3) And _bit(i And 7)) = 0 Then
j = (2*i+1) Mod 10
end_digit(x, j) += 1
x = j
c += 1
If c = 1000000 Or c = 100000000 Then
Print "first "; c; " primes"
c1 = c \ 100
For i1 = 1 To 9
For j1 = 1 To 9
x1 = end_digit(i1, j1)
If x1 <> 0 Then
Print Using frmt_str; i1; j1; x1; (x1 / c1)
End If
Next
Next
Print
If c = 100000000 Then Exit for
End If
End If
Next
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End