thirdapple(陨落雕)我有点不明白?
只要不被“2~他自己的平方根”整除就可以了例如说 15  他不能被2整除,也不能被自己的平方根整除,但他不是质数呀.是不??

解决方案 »

  1.   

    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
      

  2.   


    以前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
      

  3.   

    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
    根据上面的代码改的
      

  4.   

    我说的是chenxin790311(逍遥鱼)的代码