Home |
Search |
Today's Posts |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Nia,
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 ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct) "Nia" wrote in message ... Where can I get the function? =IsPrime(num) I typed it in a cell and got an error message. Is there an add-on for more functions in Excel? I want an IF test for a cell =IF(IsPrime(B1),"Prime","Composite") |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Largest Prime Number | Excel Discussion (Misc queries) | |||
prime number | Excel Discussion (Misc queries) | |||
How to determine the prime numbers? | Excel Discussion (Misc queries) | |||
How do I test for a prime number? | Excel Worksheet Functions | |||
PRIME FACTOR | Excel Worksheet Functions |