2010-12-27 15:28:59 +01:00
|
|
|
#!/usr/bin/env gbs3
|
|
|
|
|
|
|
|
Sub GetPrimes(N As Integer) As Integer[]
|
|
|
|
|
|
|
|
Dim aRes As New Integer[]
|
|
|
|
Dim S As Integer[]
|
|
|
|
Dim I, J, M As Integer
|
|
|
|
Dim iMRoot, iHalf As Integer
|
2015-10-31 02:02:58 +01:00
|
|
|
|
2010-12-27 15:28:59 +01:00
|
|
|
If N < 2 Then Return aRes
|
|
|
|
|
2015-10-31 02:02:58 +01:00
|
|
|
If N = 2 Then Return [2]
|
2010-12-27 15:28:59 +01:00
|
|
|
|
|
|
|
S = New Integer[(N - 3 + 1) \ 2]
|
|
|
|
For J = 3 To N Step 2
|
|
|
|
S[I] = J
|
|
|
|
Inc I
|
|
|
|
Next
|
|
|
|
|
|
|
|
iMRoot = Sqr(N)
|
2015-10-31 02:02:58 +01:00
|
|
|
iHalf = S.Max
|
2010-12-27 15:28:59 +01:00
|
|
|
I = 0
|
|
|
|
M = 3
|
2015-10-31 02:02:58 +01:00
|
|
|
|
2010-12-27 15:28:59 +01:00
|
|
|
While M <= iMRoot
|
2015-10-31 02:02:58 +01:00
|
|
|
|
2010-12-27 15:28:59 +01:00
|
|
|
If S[I] Then
|
2018-06-05 11:43:39 +02:00
|
|
|
|
2015-10-31 02:02:58 +01:00
|
|
|
For J = (M * M - 3) \ 2 To iHalf Step M
|
|
|
|
S[J] = 0
|
|
|
|
Next
|
2018-06-05 11:43:39 +02:00
|
|
|
|
2010-12-27 15:28:59 +01:00
|
|
|
Endif
|
|
|
|
|
|
|
|
Inc I
|
|
|
|
M = 2 * I + 3
|
|
|
|
|
|
|
|
Wend
|
|
|
|
|
|
|
|
aRes.Add(2)
|
2015-10-31 02:02:58 +01:00
|
|
|
For I = 0 To S.Max
|
|
|
|
J = S[I]
|
|
|
|
If J Then aRes.Add(J)
|
2010-12-27 15:28:59 +01:00
|
|
|
Next
|
|
|
|
|
|
|
|
Return aRes
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Dim aRes As Integer[]
|
|
|
|
Dim I As Integer
|
2018-06-05 11:43:39 +02:00
|
|
|
dim T as Float = Timer
|
2010-12-27 15:28:59 +01:00
|
|
|
|
2015-10-31 02:02:58 +01:00
|
|
|
For I = 1 To 5
|
2010-12-27 15:28:59 +01:00
|
|
|
aRes = GetPrimes(10000000)
|
|
|
|
Print aRes.Count
|
|
|
|
Next
|
2018-06-05 11:43:39 +02:00
|
|
|
|
|
|
|
Print Format(Timer - T, "0.000 s")
|
2018-07-03 00:54:34 +02:00
|
|
|
|
|
|
|
Error CStr(Jit.Time)
|