LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Prime Numbers?

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Largest Prime Number Gary''s Student Excel Discussion (Misc queries) 10 November 24th 08 01:26 AM
prime number danpt Excel Discussion (Misc queries) 8 October 10th 08 05:07 PM
How to determine the prime numbers? Eric Excel Discussion (Misc queries) 9 October 4th 07 07:48 PM
How do I test for a prime number? Stephen P Thomas Excel Worksheet Functions 3 July 13th 05 11:43 PM
PRIME FACTOR Ted Moise Excel Worksheet Functions 1 October 29th 04 06:40 AM


All times are GMT +1. The time now is 09:50 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"