Guest User

Untitled

a guest
Jul 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. def miller_rabin(n, k=10):
  2. if n == 2:
  3. return True
  4. if not n & 1:
  5. return False
  6.  
  7. def check(a, s, d, n):
  8. x = pow(a, d, n)
  9. if x == 1:
  10. return True
  11. for i in xrange(s - 1):
  12. if x == n - 1:
  13. return True
  14. x = pow(x, 2, n)
  15. return x == n - 1
  16.  
  17. s = 0
  18. d = n - 1
  19.  
  20. while d % 2 == 0:
  21. d >>= 1
  22. s += 1
  23.  
  24. for i in xrange(k):
  25. a = randrange(2, n - 1)
  26. if not check(a, s, d, n):
  27. return False
  28. return True
  29.  
  30. # benchmark of 10000 iterations of miller_rabin(100**10-1); Which is not prime.
  31.  
  32. # 10000 calls, 11111 per second.
  33. # 74800 function calls in 0.902 seconds
Add Comment
Please, Sign In to add comment