Here is a routine originally presented by Myrna Larson that I use to test
for primes.
=IsPrime(num)
returns True or False
Function IsPrime(TestNum As Long)
Dim PrimeCnt As Long
Dim y As Long
Dim x As Long
Dim i As Long
Dim Flag As Boolean
Dim Primes() As Long
Dim NumStop As Double
ReDim Primes(1 To 2)
NumStop = Sqr(TestNum)
If TestNum = 1 Or TestNum = 2 Or TestNum = 3 Or TestNum = 5 Then
IsPrime = True
Exit Function
End If
Primes(1) = 2
Primes(2) = 3
PrimeCnt = 2
x = 3
Do
x = x + 2
For y = 3 To Sqr(x) Step 2
If x Mod y = 0 Then GoTo NoPrime1
Next y
PrimeCnt = PrimeCnt + 1
ReDim Preserve Primes(1 To PrimeCnt)
Primes(PrimeCnt) = x
NoPrime1:
Loop Until Primes(PrimeCnt) NumStop
For i = LBound(Primes) To UBound(Primes)
If TestNum Mod Primes(i) = 0 Then
IsPrime = False
Exit Function
End If
Next
IsPrime = True
End Function
--
HTH
Bob Phillips
"Stephen P Thomas" wrote in
message ...
Aside from a lookup table of primes, is there any neater way of detecting
that a number is prime?
|