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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Got this from Daily Dose of Excel » Blog Archive » Is this number prime? and modified slightly :)

Code:
Function IsPrime(Number As Single) As Integer
    If Number < 2 Or (Number > 2 And Number Mod 2 = 0) Or Number <> Int(Number) Then Exit Function
    For I = 3 To Sqr(Number)
        If Number Mod I = 0 Then Exit Function
    Next
    IsPrime = 1
End Function

And a small example here

Excel 2010
GH
5NumberPrime or Not
6131
7111
8150
9160
10171
11191
12210
13231
14340
15350
16311
Sheet6
Cell Formulas
RangeFormula
H6=isprime(G6)
 
Upvote 0
I tried to use that function in the following macro, but I got an error. Can anyone see my problem?

Function IsPrime(Number As Single) As Integer
If Number < 2 Or (Number > 2 And Number Mod 2 = 0) Or Number <> Int(Number) Then Exit Function
For i = 3 To Sqr(Number)
If Number Mod i = 0 Then Exit Function
Next
IsPrime = 1
End Function

Sub CheckForTriplets()

Dim i, n, check As Long

For i = 5 To 1000 Step 2
check = IsPrime(i) + IsPrime(i + 2) + IsPrime(i + 6)
If check = 3 Then report = report + 1
Next
MsgBox report
End Sub
 
Upvote 0
Try dimensioning i as Single:
Rich (BB code):
Sub CheckForTriplets()

 Dim i as Single, check As Long

 For i = 5 To 1000 Step 2
 check = IsPrime(i) + IsPrime(i + 2) + IsPrime(i + 6)
 If check = 3 Then report = report + 1
 Next
 MsgBox report
 End Sub
 
Upvote 0
A little longer ...

Code:
Function IsPrime(d As Double, Optional bFirstFactor As Boolean = False) As Variant
  '   Returns                    if d is
  ' -----------  ----------------------------------------------
  '  #VALUE!      < 2
  '  "Too big!"   > 1E+15
  '  #VALUE!      <> Int(d)
  '  FALSE        composite and bFirstFactor = False or omitted
  '  1st factor   composite and bFirstFactor = True
  '  TRUE         prime

  Dim dDiv  As Double
  Dim dRt   As Long

  If d < 2# Then
    IsPrime = CVErr(xlErrValue)
    Exit Function  
  ElseIf d = 2# Or d = 5# Then
    IsPrime = True
    Exit Function  
  ElseIf d > 1000000000000000# Then
    IsPrime = "Too big!"
    Exit Function  
  ElseIf Int(d) <> d Then
    IsPrime = CVErr(xlErrValue)
    Exit Function  
  End If

  ' can't use Mod with numbers bigger than Longs, so ...
  Select Case Right(Format(d, "0"), 1)
    Case "0", "2", "4", "6", "8"
      IsPrime = IIf(bFirstFactor, 2, False)
    Case "5"
      IsPrime = IIf(bFirstFactor, 5, False)
    Case Else
      For dRt = 3 To Int(Sqr(d)) Step 2
        dDiv = d / dRt
        If Int(dDiv) = dDiv Then
          IsPrime = IIf(bFirstFactor, dRt, False)
          Exit Function  
        End If
      Next dRt
      IsPrime = True
  End Select
End Function
 
Upvote 0
@shg:

If bored and/or it's not too painful, would you respond to Rick's question? I recall a thread where this was discussed, but that is where my blond memory stops. I do not recall whether it was the n in For n = [some value] To [some other value] that was the issue, or whether it was where the called Function's parameter n was affected, or ?????

Thank you so much regardless of whether you get to reply or not.

Mark
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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