Guest User

Untitled

a guest
Aug 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. import random, decimal, time
  2. from fractions import gcd
  3.  
  4. D = decimal.Decimal
  5. decimal.getcontext().prec = 50
  6.  
  7. #v11 = 5689 71935 26942 02437 03269 72321
  8.  
  9. def tword( p ):
  10. """ Changes the number to the form 2^r * d. Outputs
  11. r and d in a list. [r, d]"""
  12.  
  13. #possible prime to be sorted to form p = 2^r * d
  14. r = 0
  15. d = 0
  16.  
  17. while (p % pow(2, r+1)) == 0:
  18. r += 1
  19. d = p / pow(2, r)
  20. return [r, d]
  21.  
  22. def testA( p, a, r, d ):
  23.  
  24. rd = tword( p - 1 )
  25. r = rd[0]
  26. d = rd[1]
  27. #print "r: ", r
  28. #print "d: ", d
  29.  
  30. i = 1;
  31.  
  32. test = pow(a, d, p)
  33.  
  34. if test == 1 or test == p - 1: #if (p^d != 1) mod p
  35. return 1
  36.  
  37. #print "i:", i
  38. #print "r:", r
  39. while i < r:
  40. #print pow(a, pow(2, i)*d, p)
  41. if pow(a, pow(2, i)*d, p) == p-1: #if ( a^(2^r *d) != (p - 1) ) mod p
  42. return 1
  43. i += 1
  44. return 0
  45.  
  46. def millerRabin( p, k ):
  47. i = 0;
  48.  
  49. rd = tword( p - 1 )
  50. r = rd[0]
  51. d = rd[1]
  52. elapsed = 0;
  53. gcd_elapsed = 0;
  54. start = time.clock();
  55.  
  56. while i < k:
  57. #print "Spot"
  58. a = random.randint(2, p-1)
  59. if a%2 == 0:
  60. a += 1
  61. start_segment = time.clock();
  62. if gcd( p, a ) != 1:
  63. print "This is composite."
  64. return 0
  65. end_segment = time.clock();
  66. gcd_elapsed += end_segment - start_segment
  67.  
  68.  
  69. if testA(p, a, r, d) == 1: #if base returns prime
  70. i += 1 #try another base
  71. else:
  72. print "Not prime. a =", a;
  73. return 0
  74. end = time.clock()
  75. elapsed = end - start;
  76. #print "The gcd function took", (gcd_elapsed / elapsed)*100, "% of \
  77. #the total time."
  78. print "It took", elapsed, " seconds."
  79. print "Probably prime, with", D(D(1) - D( str(pow(4, -k)) ))*D(100), " % certainty."
  80. return 1
  81.  
  82. def millerRabinBases( p, bases ):
  83. print "bases"
  84. i = 0;
  85.  
  86. rd = tword( p - 1 )
  87. r = rd[0]
  88. d = rd[1]
  89. elapsed = 0;
  90. start = time.clock();
  91.  
  92. for a in bases:
  93. #print "Spot"
  94.  
  95. if testA(p, a, r, d) == 1: #if base returns prime
  96. i += 1 #try another base
  97. else:
  98. #print "Not prime. a =", a;
  99. return 0
  100. end = time.clock()
  101. elapsed = end - start;
  102. #print "The gcd function took", (gcd_elapsed / elapsed)*100, "% of \
  103. #the total time."
  104. print "It took", elapsed, " seconds."
  105. return 1
  106.  
  107. def millerRabinProof( p ):
  108. """Determines if number "p" is prime. P must be a 336 digit
  109. number or less. This is faster than any other algorithm
  110. I think."""
  111. if p < 1373653: #7 digits. V2
  112. return millerRabinBases( p, [2, 3] );
  113. elif p < 4759123141: #10 digits
  114. return millerRabinBases( p, [2, 7, 61] )
  115. elif p < 41550071728321: #14 digits. V7
  116. return millerRabinBases( p, [2, 3, 5, 7, 11, 13, 17] )
  117. elif p < 41234316135705689041: #20 digits. V9: smallest strong pseudoprime
  118. print "V9"
  119. return millerRabinBases( p, sieve.sieveSmall(9))#to first 9 prime bases
  120. elif p < 1955097530374556503981: #22 digits. V10
  121. return millerRabinBases( p, sieve.sieveSmall(10))
  122. elif p < 7395010240794120709381: #22 digits. V11
  123. return millerRabinBases( p, sieve.sieveSmall(11))
  124. elif p < 318665857834031151167461: #24 digits. V12
  125. return millerRabinBases( p, sieve.sieveSmall(12))
  126. elif p < 8038374574536394912570796143419421081388376882875581458374889175222974273765333652186502336163960045457915042023603208766569966760987284043965408232928738791850869166857328267761771029389697739470167082304286871099974399765441448453411558724506334092790222752962294149842306881685404326457534018329786111298960644845216191652872597534901:
  127. #337 digits. V46
  128. return millerRabinBases( p, sieve.sieveSmall(46))
  129. else:
  130. return "This number is too big. It must be 336 digits or less."
  131.  
  132. def millerRabinGRH( p ):
  133. """Input: An integer to be tested for primality.
  134. Assuming the Generalized Riemann Hypothesis is true, this algorithm
  135. proves an integer is prime."""
  136. import math
  137.  
  138. return millerRabinBases( p, [2] + range(3, math.ceil(2*pow(math.log(p), 2))))
  139.  
  140. def millerRabinGRHsieve( p ):
  141. """Input: An integer to be tested for primality.
  142. Assuming the Generalized Riemann Hypothesis is true, this algorithm
  143. proves an integer is prime."""
  144. import math
  145.  
  146. return millerRabinBases( p, sieve.sieve(math.ceil((2*pow(math.log(p), 2)))))
  147.  
  148. def naivePrime( p ):
  149. import math
  150. i = 3;
  151. while i <= math.ceil(math.sqrt(p)):
  152. if p % i == 0:
  153. return 0
  154. i += 2
  155. return 1
Add Comment
Please, Sign In to add comment