One way:
|
A |
B |
C |
1 |
264,627,858,537,807 |
3 |
B1: =isprime(A1, TRUE) |
2 |
250,522,486,845,971 |
6719759 |
|
3 |
336,502,419,415,881 |
3 |
|
4 |
213,751,729,707,615 |
5 |
|
5 |
215,771,119,714,057 |
164513 |
|
6 |
384,515,184,772,903 |
17 |
|
7 |
383,451,182,203,411 |
7 |
|
8 |
234,532,507,205,987 |
TRUE |
|
9 |
328,100,503,187,517 |
3 |
|
10 |
200,768,597,700,379 |
17 |
|
Function IsPrime(dNum As Double, Optional bFirstFactor As Boolean = False) As Variant
' Returns if dNum is
' ----------- -----------------------------------
' #VALUE! <> Int(dNum) or dNum < 2
' "Too big!" > 1E+15
' FALSE composite and bFirstFactor = False
' 1st factor composite and bFirstFactor = True
' TRUE prime
Dim dDiv As Double ' divisor
Dim dQuo As Double ' quotient
If Int(dNum) <> dNum Then
IsPrime = CVErr(xlErrValue)
Else
If dNum < 2# Then
IsPrime = CVErr(xlErrValue)
Else
If dNum = 2# Or dNum = 5# Then
IsPrime = True
Else
If dNum > 1E+15 Then
IsPrime = "Too big!"
Else
Select Case Right(Format(dNum, "0"), 1)
Case "0", "2", "4", "6", "8"
IsPrime = IIf(bFirstFactor, 2#, False)
Case "5"
IsPrime = IIf(bFirstFactor, 5#, False)
Case Else
' can't use Mod with numbers bigger than Longs, so ...
For dDiv = 3# To Sqr(dNum) Step 2#
dQuo = dNum / dDiv
If Int(dQuo) = dQuo Then
IsPrime = IIf(bFirstFactor, dDiv, False)
Exit Function
End If
Next dDiv
IsPrime = True
End Select
End If
End If
End If
End If
End Function
Bookmarks