This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Code

By: a guest on Jun 12th, 2013  |  syntax: VisualBasic  |  size: 1.32 KB  |  views: 47  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Function Miller_Rabin(ByVal n As Integer) As Boolean
  2.         If n <= 3 Then : Return True
  3.         ElseIf n Mod 2 = 0 Then : Return False
  4.         Else
  5.             Dim k, s, a, d, x As Integer
  6.             k = 3
  7.             d = n - 1
  8.  
  9.             While d Mod 2 = 0
  10.                 d = d / 2
  11.                 s += 1
  12.             End While
  13.  
  14.             For c = 1 To k
  15.                 a = Random(2, n - 1)
  16.                 x = a ^ d Mod n
  17.                 If x = 1 Or x = n - 1 Then GoTo skip
  18.                 For r = 1 To s - 1
  19.                     x = x ^ 2 Mod n
  20.                     If x = 1 Then
  21.                         Return False
  22.                         Exit Function
  23.                     Else
  24.                         If x = n - 1 Then
  25.                             GoTo skip
  26.                         Else
  27.                             Return False
  28.                             Exit Function
  29.                         End If
  30.                     End If
  31.                 Next
  32. skip:       Next
  33.             Return True
  34.         End If
  35.     End Function
  36.  
  37.     Function Random(ByVal x As Integer, ByVal n As Integer) As Integer
  38.         Dim a As Integer = Now.Millisecond * Now.Second
  39. skip:
  40.         a = (a ^ 2 + 1) Mod (n + 1)
  41.         If a < x Then
  42.             GoTo skip
  43.         Else
  44.             Return a
  45.         End If
  46.     End Function
clone this paste RAW Paste Data