Is this number prime?

In one of my (many) previous lives, I wanted to be a mathematician.

Even after I realized that being a mathematician would preclude a lifestyle that I wanted to become accustomed to, I still retained an active interest in mathematics. One of the areas that I found fascinating involved prime numbers.

I've seen many UDFs in the newsgroups, mostly provided by Excel MVPs, to determine if a number is prime. However, the following is the shortest UDF that I've been able to come up with:

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

The reason for declaring the input number Num as a Single is that if it's declared as an Integer or a Long, an input value of, say, 3.2 will be coerced to 3 and be evaluated as a prime number. Clearly, this is not the desired result. There is probably a better way of handling this, but I can't figure it out.

I'd be interested in some comments as to whether this code can be shortened or made more elegant.

7 Comments

  1. Eric W. Bachtal:

    Couple of quick notes:
    - break up the compound conditional statements; VBA doesn't short-circuit, so you'll be doing more work than necessary unless you give each condition its own IF
    - use Fix instead of Int to avoid the overhead of unnecessary (bankers) rounding
    - before embarking on a whole lot of MODs, consider adding the digits in the number together and MOD'ng with 3
    - another quick saver is to store an array of known primes (up to 200, let's say) and divide your prospective prime by them before embarking on a loop

  2. Eric W. Bachtal:

    Oops, forget the Int/Fix comment -- my 3AM brain was thinking CInt.

  3. Paul Hoch:

    I wonder how large a number you can use without clogging up your cpu resources. A long time ago, I used to run a program to check for mersenne prime numbers (2^prime number - 1) and it used to take weeks to determine the answer. For more interesting info on Mersenne prime numbers you can check out this site - http://www.utm.edu/research/primes/mersenne/

    In case you're wondering I sometimes still want to be a mathematician. It fascinates me.

  4. Steve:

    This seems to work for primes > 3.

    If Num Int(Num) Then Exit Function
    Select Case Num Mod 6
    Case 1, 5
    Dim i As Long
    For i = 5 To Sqr(Num) Step 6
    If Num Mod i = 0 Then Exit Function
    If Num Mod i + 2 = 0 Then Exit Function
    Next i
    Case Else
    Exit Function
    End Select
    IsPrime = True

  5. Devyn:

    I dont understand why you wouldnt just put on a graph of prime #s
    u absoulutley need to do this! plz and thanx
    Devyn Fortunati

  6. Sean McCloskey:

    It's faster just to divide by known primes up to Sqr(Num) as Eric Bachtal says. No need to divide by all odd number or all numbers that are congruent to 1 mod 6 and 5 mod 6. Faster still if you are trying to find all prime numbers in a certain range (so I've heard -- I plan to test this out myself when I have a chance) is to set up a Sieve of Erastothenes, which eliminates numbers that aren't prime, rather than testing every number for primeness.

  7. logic:

    the fastest way to find consecutive primes is to create an array and cross out all the non prim numbes.

    any way here is a very basic prime checker

    http://logic-dust.blogspot.com/2006/08/primary-number-checker.html

Leave a comment