Option Explicit
'' ***************************************************************************
'' Purpose : Test whether the argument is a PRIME number
'' Written : January 98 by Andy Wiggins - Byg Software Ltd
'' Notes : Returns TRUE or a zero length string
''
Function IsPrime(vlTestNumber As Long)
''Application.Volatile ''Not essential for this demo
Dim vlCount As Long ''Declare some variables
Dim vlHalf As Long
vlHalf = vlTestNumber / 2 + 1 ''By using a LONG we get an integer result, which is what we want
For vlCount = 2 To vlHalf ''Loop until we get to vlHalf
If (vlTestNumber Mod vlCount) = 0 Then ''If we get a ZERO result in this test, the number is not a prime ..
IsPrime = "" ''.. so return an empty string ..
Exit Function ''.. and leave the function
End If
Next
IsPrime = True ''If we get here, the number is a prime, so return TRUE
End Function
'' ***************************************************************************
'' Purpose : Test whether the argument is a PRIME number
'' Written : January 98 by Andy Wiggins - Byg Software Ltd
'' Notes : Returns TRUE or FALSE
''
Function IsPrimeBool(vlTestNumber As Long) As Boolean
''Application.Volatile ''Not essential for this demo
Dim vlCount As Long
Dim vlHalf As Long
vlHalf = vlTestNumber / 2 + 1 ''By using a LONG we get an integer result, which is what we want
For vlCount = 2 To vlHalf ''Loop until we get to vlHalf
If (vlTestNumber Mod vlCount) = 0 Then ''If we get a ZERO result in this test, the number is not a prime ..
IsPrimeBool = False ''.. so return FALSE
Exit Function ''.. and leave the function
End If
Next
IsPrimeBool = True ''If we get here, the number is a prime, so return TRUE
End Function