vbs问题高手帮忙!

来源:百度知道 编辑:UC知道 时间:2024/05/12 16:21:18
写一个求100以内素数个数的VBS。

For i = 3 To 100 Step 2
For j = 2 To Sqr(i)
If i Mod j = 0 Then Exit For
Next
If j > Sqr(i) Then WScript.Echo i
Next

存成VBS运行

Function CalcPrimes(intPrimeUBound)
'intPrimeUBound 是要计算素数的上限值
Dim i, j
Dim strTmp
Dim bitArray

Redim bitArray(intPrimeUBound + 1)

For i = 0 To intPrimeUBound
bitArray(i) = 1
Next

For i = 2 To CInt(Sqr(intPrimeUBound))
If 1 = bitArray(i) Then
j = i
While j*i <= intPrimeUBound
bitArray(i*j) = 0
j = j + 1
Wend
End If
Next

For i = 2 To intPrimeUBound
If 1 = bitArray(i) Then
strTmp = strTmp & CStr(i) & ","
End If
Next

If strTmp <> "" Then
strTmp = Left(strTmp, Len(strTmp) - 1)
CalcPrimes = Split(strTmp, ",")
Else
CalcPrimes = ""
End If
End Function