Results 1 to 17 of 17

Thread: Rabin-Miller Primality Test

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Question Rabin-Miller Primality Test

    I'm having problems transposing this pseudo-code into something usable. Perhaps someone could lend a hand?

    Pseudo-code:
    Input: n > 2, an odd integer to be tested for primality;

    Input: k, a parameter that determines the accuracy of the test

    Output: composite if n is composite, otherwise probably prime

    write n − 1 as 2s·d with d odd by factoring powers of 2 from n − 1

    LOOP: repeat k times:
    pick a randomly in the range [2, n − 1]
    x = ad mod n
    if x = 1 or x = n − 1 then do next LOOP
    for r = 1 .. s − 1
    x = x2 mod n
    if x = 1 then return composite
    if x = n − 1 then do next LOOP
    return composite
    return probably prime
    What I have:
    Code:
    Private Sub Form_Load()
    
    Dim K As Long
    Dim N As Long
    Dim A As Long
    Dim X As Long
    Dim D As Long
    
    Dim Prime As Boolean
    
    N = 23
    K = 11
    D = 3
    Prime = True
    
    For A = 3 To K Step 2
        DoEvents
        X = (A * D) Mod N
        If X < (X ^ 2) Mod N Then
            If X = 1 Then
                Prime = False
            End If
        End If
    Next
    
    Debug.Print Prime
    End
    
    End Sub
    I am manually assigning numbers for testing. I use "DoEvents" in case I end up with a run-away loop so I can stop it without a crash.

    Last edited by storm5510; Sep 14th, 2009 at 01:53 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width