素数生成器的算法【ASP Edtion】
1<%
2 Function CalcPrimes(intPrimeUBound)
3 'intPrimeUBound 是要计算素数的上限值
4 Dim i, j
5 Dim strTmp
6 Dim bitArray
7
8 Redim bitArray(intPrimeUBound + 1)
9
10 For i = 0 To intPrimeUBound
11 bitArray(i) = 1
12 Next
13
14 For i = 2 To CInt(Sqr(intPrimeUBound))
15 If 1 = bitArray(i) Then
16 j = i
17 While j*i <= intPrimeUBound
18 bitArray(i*j) = 0
19 j = j + 1
20 Wend
21 End If
22 Next
23
24 For i = 2 To intPrimeUBound
25 If 1 = bitArray(i) Then
26 strTmp = strTmp & CStr(i) & ","
27 End If
28 Next
29
30 If strTmp <> "" Then
31 strTmp = Left(strTmp, Len(strTmp) - 1)
32 CalcPrimes = Split(strTmp, ",")
33 Else
34 CalcPrimes = ""
35 End If
36
37 End Function
38%>
2 Function CalcPrimes(intPrimeUBound)
3 'intPrimeUBound 是要计算素数的上限值
4 Dim i, j
5 Dim strTmp
6 Dim bitArray
7
8 Redim bitArray(intPrimeUBound + 1)
9
10 For i = 0 To intPrimeUBound
11 bitArray(i) = 1
12 Next
13
14 For i = 2 To CInt(Sqr(intPrimeUBound))
15 If 1 = bitArray(i) Then
16 j = i
17 While j*i <= intPrimeUBound
18 bitArray(i*j) = 0
19 j = j + 1
20 Wend
21 End If
22 Next
23
24 For i = 2 To intPrimeUBound
25 If 1 = bitArray(i) Then
26 strTmp = strTmp & CStr(i) & ","
27 End If
28 Next
29
30 If strTmp <> "" Then
31 strTmp = Left(strTmp, Len(strTmp) - 1)
32 CalcPrimes = Split(strTmp, ",")
33 Else
34 CalcPrimes = ""
35 End If
36
37 End Function
38%>
作者: 三角猫
http://www.zu14.cn/
转载请保留此信息