Udf (or actual function) to tell if a number is prime.

Gene Klein

New Member
Joined
Feb 6, 2015
Messages
18
I am sure I asked this years ago, but I am looking for a function that determines if a number is prime. It can be a UDF or an actual built in function. (I tried =isprime of course)

It would work like this =isprime(5) would return 1
=isprime(6) would return 0

Gene Klein
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I don't think that was the cause of my original issue. I never had decimals as input.

Gene
I wasn't answering your question, I was answering Rick's/Mark's :)
And that is why one should use "Reply With Quote" (removing any non-relevant parts of the quoted message) when there are multiple respondents in a thread so everyone knows who a response is directed at.



At a guess because Prime numbers have to be whole numbers, if declared as long decimals will be coerced into their whole number value - 5.3 would become 5 for example which would return as Prime when it is clearly not.

The input as Single prevents this

shg's function addresses with:
Rich (BB code):
  ElseIf Int(d) <> d Then
    IsPrime = CVErr(xlErrValue)
    Exit Function  
  End If
Okay, I see your point, but theoretically no one should be asking if a calculated floating point number is prime given the vagaries of significant digits out around the 14th/15th decimal position when floating point values are converted to binary values (what a computer does internally to perform floating point math). However, given that, shg's use of Double is much better than the original function's use of Single (try any IsPrime function, where the argument is declared as Single, with an assumed calculated value like 11.0000000999).
 
Upvote 0

Forum statistics

Threads
1,216,085
Messages
6,128,732
Members
449,465
Latest member
TAKLAM

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top