Prime number test Mod overflow error

whyhellothere

New Member
Joined
Sep 16, 2011
Messages
1
I'm trying the google, "find the first 10 digit prime in the digits of PI problem" and I'm stuck. I have the logic for it but i keep running into mod errors.
Some of this code is lifted as I crashed while doing my last itteration and am too fried to rewrite. Any help is appreciated.

Sub prime()
Dim N As Double
Dim t As Double
Dim i As Double
Dim m As String
Dim q As Double
i = 0
m = Range("A1")
q = 10
For q = 10 To 100
t = Mid(m, 2, q)
Range("A2") = t

Dim bPrime As Boolean
For N = 2 To t ' Lower and Upper are entered by user
bPrime = True ' continue testing as long as true
Dim F As Integer
For F = 2 To t - 1 ' test all factors from 2 to N-1
If t Mod F = 0 Then
bPrime = False ' divisible, therefore composite
Exit For ' no more testing required
End If
Next
If bPrime = True Then ' If never got a factor, Prime found
Cells(5, 1).Value = t
End If
Next
Next
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Welcome to the board.

Here's the function I use for testing primality:

Code:
Function FirstFactor(dNum As Double) As Variant
    ' shg 2006-0923
    '     2009-1003 fixed bug so if dNum Mod 15 = 0, returns 3 as a factor, not 5
    ' Returns:      if dNum is:
    '   #VALUE!       < 2
    '   "Too big!"    > 1E+15
    '   #VALUE!       <> Int(dNum)
    '   1st factor    otherwise (=dNum if prime)
 
    Dim dQuo        As Double
    Dim dFac        As Double
 
    If Int(dNum) <> dNum Then
        FirstFactor = CVErr(xlErrValue)
    ElseIf dNum > 1E+15 Then
        FirstFactor = "Too big!"
    ElseIf Int(dNum / 2#) = dNum / 2 Then
        FirstFactor = 2#
    Else
        For dFac = 3# To Int(Sqr(dNum)) Step 2#
            dQuo = dNum / dFac
            If Int(dQuo) = dQuo Then
                FirstFactor = dFac
                Exit Function    '------------------------------------->
            End If
        Next dFac
        FirstFactor = dNum
    End If
End Function

EDIT: You can't use MOD because it converts its arguments to Longs, which will overflow for most 10-digit numbers.
 
Last edited:
Upvote 0
Hi,

I googled your bit in quotes and didn't come up with anything except your post in this thread.

So I don't know what your specific problem actually is, thus can't offer much towards solving it.

But I put int(pi()*10^14) in Range("A1") and ran your code but didn't attain enlightenment.

The general problem of determining if a 10 digit number is prime or not is pretty straightforward and needn't take long, but I don't know what your problem actually is ...

Like, if you want primes, and prime factors, for numbers from 10^9+101 to 10^9+130, the below took me about 0.05secs to calculate and display. Primes only would take less time.
1000000101 3 333333367
1000000102 2 83 6024097
1000000103 1000000103(prime)
1000000104 2 2 2 3 41666671
1000000105 5 13 15384617
1000000106 2 7 47 521 2917
1000000107 3 3 3 211 257 683
1000000108 2 2 31 8064517
1000000109 20063 49843
1000000110 2 3 5 37 163 5527
1000000111 11 90909101
1000000112 2 2 2 2 17 17 216263
1000000113 3 7 47619053
1000000114 2 500000057
1000000115 5 19 10526317
1000000116 2 2 3 3 27777781
1000000117 103 109 89071
1000000118 2 13 23 233 7177
1000000119 3 6421 51913
1000000120 2 2 2 5 7 3571429
1000000121 3463 288767
1000000122 2 3 11 2089 7253
1000000123 1000000123(prime)
1000000124 2 2 397 629723
1000000125 3 3 5 5 5 67 13267
1000000126 2 12251 40813
1000000127 7 29 41 137 877
1000000128 2 2 2 2 2 2 2 3 2604167
1000000129 17 58823537
1000000130 2 5 827 120919
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,248
Members
452,900
Latest member
LisaGo

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