Thread: prime number
View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.misc
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default prime number

The number 1 isn't a prime number. I can't see how to adjust the formula
other than with an additional IF statement

=IF(C8=1,"not prime",IF(OR(C8=2,C8=3),"prime",
IF(AND((MOD(C8,ROW(INDIRECT("2:"&C8-1)))<0)),"prime","not prime")))

or perhaps a UDF

Function IsPrime(Number As Single) As Boolean
Dim x As Long
If Number < 2 Or (Number < 2 And Number Mod 2 = 0) _
Or Number < Int(Number) Then Exit Function
For x = 3 To Sqr(Number) Step 2
If Number Mod x = 0 Then Exit Function
Next
IsPrime = True
End Function

Mike

"Sheeloo" wrote:

No.

Thanks to Chip Pearson for providing this formula
________________
The formula below will test the number in cell C8 and return the word prime
if it is prime or the string not prime if the number is not prime.

=IF(OR(C8=1,C8=2,C8=3),"prime",
IF(AND((MOD(C8,ROW(INDIRECT("2:"&C8-1)))<0)),"prime","not prime"))

This is an array formula, so you must press CTRL SHIFT ENTER rather than
just ENTER when you first enter the formula and whenever you edit it later.
If you do this properly, Excel will display the formula enclosed in curly
braces { }.
________________
For details go to http://www.cpearson.com/excel/PrimeNumbers.aspx

"danpt" wrote:

Does Excel has a formula to check if an integer is a prime number or not?
Thank you