IsPrimeNumber




Function IsPrime2(lngNumber As Long) As Boolean
Dim lngCount As Long' low range To check
Dim lngSqr As Long ' holds square root of target
Dim x As Long' temp Loop counter
lngSqr = Sqr(lngNumber) ' Get the int square root

If lngNumber < 2 Then' special Case if 0 or 1 (NOT PRIME!)
IsPrime2 = False
Exit Function
End If

lngCount = 2' Special Case To see If even number
IsPrime2 = True ' assume primality

If lngNumber Mod lngCount = 0& Then ' evenly divisible?
IsPrime2 = False ' Not a prime
Exit Function' abort!
End If

lngCount = 3' primes the pump (ooh, sorry!)

For x& = lngCount To lngSqr Step 2 ' check all odd numbers now
If lngNumber Mod x& = 0 Then ' evenly divisible?
IsPrime2 = False ' Not a prime
Exit Function ' abort!
End If
Next

End Function











( isprimenumber.html )- by Paolo Puglisi - Modifica del 17/12/2023