Function Judge(ByVal Number As Integer) As Boolean '返回true的就是质数 Dim i As Long Judge = False If Abs(Number) = 0 Then Exit Function '是0,当然不是质数 For i = 2 To 19 If Number Mod i = 0 Then If i <> Number Then Exit Function End If End If Next Judge = True End Function
以前Bardo(巴顿)写的:--------------------------------------------------------------------Private Function GetPrimeNum(ByVal lMax As Long) As Variant '快速生成素数的程序(因为,在我见到的素数生成程序中还没有一个快速的) '很多人写程序,从不考虑算法.以下即是一例.此例用了动态数组大大减少程序的运 算量 '因为,素数是对于其不大于本身的二次方根的所有素数都不能整除的数. '因而,用动态数组可以只用当前所获素数去检验一个新的数,而不要用所有奇数去检 验 '******************************************************** '程序为本人原创 '******************************************************** '以下程序返回一个变体素数数组,参数是要求的最大数'以此算法,计算100000以内的素数耗时仅1740毫秒!!!!!! '不过将其Join为字串在TextBox中显示却用了近5秒 '计算10000以内的素数耗时仅90毫秒!!!!!!'此处定义为String以便于函数外部用Join函数,你也可以用Long Dim PrimeNum() As String Dim i As Long, j As Long Dim isNotPrimeNum As Boolean Dim lRootNum As LongReDim PrimeNum(0)PrimeNum(0) = 3 For i = 5 To lMax Step 2 lRootNum = Sqr(i) For j = 0 To UBound(PrimeNum) isNotPrimeNum = False If PrimeNum(j) > lRootNum Then Exit For End If If i Mod PrimeNum(j) = 0 Then isNotPrimeNum = True Exit For End If Next j If isNotPrimeNum = False Then ReDim Preserve PrimeNum(UBound(PrimeNum) + 1) PrimeNum(UBound(PrimeNum)) = i End If Next iGetPrimeNum = PrimeNumEnd Function
Function Judge(ByVal Number As Long) As Boolean '返回true的就是质数 Dim i As Long Judge = False Number = Abs(Number) If Number = 0 Then Exit Function '是0,当然不是质数 For i = 2 To Sqr(Number) If Number Mod i = 0 Then Exit Function Next i Judge = True End Function 根据上面的代码改的
'返回true的就是质数
Dim i As Long
Judge = False
If Abs(Number) = 0 Then Exit Function '是0,当然不是质数
For i = 2 To 19
If Number Mod i = 0 Then
If i <> Number Then
Exit Function
End If
End If
Next
Judge = True
End Function
以前Bardo(巴顿)写的:--------------------------------------------------------------------Private Function GetPrimeNum(ByVal lMax As Long) As Variant
'快速生成素数的程序(因为,在我见到的素数生成程序中还没有一个快速的)
'很多人写程序,从不考虑算法.以下即是一例.此例用了动态数组大大减少程序的运
算量
'因为,素数是对于其不大于本身的二次方根的所有素数都不能整除的数.
'因而,用动态数组可以只用当前所获素数去检验一个新的数,而不要用所有奇数去检
验
'********************************************************
'程序为本人原创
'********************************************************
'以下程序返回一个变体素数数组,参数是要求的最大数'以此算法,计算100000以内的素数耗时仅1740毫秒!!!!!!
'不过将其Join为字串在TextBox中显示却用了近5秒
'计算10000以内的素数耗时仅90毫秒!!!!!!'此处定义为String以便于函数外部用Join函数,你也可以用Long
Dim PrimeNum() As String
Dim i As Long, j As Long
Dim isNotPrimeNum As Boolean
Dim lRootNum As LongReDim PrimeNum(0)PrimeNum(0) = 3
For i = 5 To lMax Step 2
lRootNum = Sqr(i)
For j = 0 To UBound(PrimeNum)
isNotPrimeNum = False
If PrimeNum(j) > lRootNum Then
Exit For
End If
If i Mod PrimeNum(j) = 0 Then
isNotPrimeNum = True
Exit For
End If
Next j
If isNotPrimeNum = False Then
ReDim Preserve PrimeNum(UBound(PrimeNum) + 1)
PrimeNum(UBound(PrimeNum)) = i
End If
Next iGetPrimeNum = PrimeNumEnd Function
'返回true的就是质数
Dim i As Long
Judge = False
Number = Abs(Number)
If Number = 0 Then Exit Function '是0,当然不是质数
For i = 2 To Sqr(Number)
If Number Mod i = 0 Then Exit Function
Next i
Judge = True
End Function
根据上面的代码改的