alexpanoiu

Find Good Multiplier

Dec 5th, 2018 (edited)
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub FindGoodMultiplier( _
  2.   ByVal Modulus As Long, _
  3.   Optional ByVal OverflowAt As Long = 32767)
  4.  
  5.   Dim Multiplier As Long
  6.   Dim Hit() As Boolean
  7.   Dim m As Long
  8.   Dim x As Long
  9.   Dim i As Long
  10.  
  11.   Do While Modulus > 1
  12.     ReDim Hit(1 To Modulus - 1) As Boolean
  13.     m = OverflowAt \ Modulus
  14.     If m >= Int(Modulus ^ (2 / 3)) Then m = Int(Modulus ^ (2 / 3))
  15.     For Multiplier = m To 2 Step -1
  16.       For i = 1 To Modulus - 1: Hit(i) = False: Next i
  17.       x = 1
  18.       For i = 2 To Modulus - 1
  19.         If x = 0 Then Exit For
  20.         If Hit(x) Then Exit For
  21.         Hit(x) = True
  22.         x = (x * Multiplier) Mod Modulus
  23.       Next i
  24.       If i > Modulus - 1 Then
  25.         Debug.Print Multiplier & " is a good multiplier for modulus " & Modulus
  26.         Exit Sub
  27.       End If
  28.     Next Multiplier
  29.     Modulus = Modulus - 1
  30.     DoEvents
  31.   Loop
  32.  
  33.   Debug.Print "No good multiplier was found"
  34.  
  35. End Sub
  36.  
  37. ' 1020 is a good multiplier for modulus 32749
  38. ' 63 is a good multiplier for modulus 509
  39. ' 21 is a good multiplier for modulus 97
  40. ' 23 is a good multiplier for modulus 127
Advertisement