Miller Rabin


SUBMITTED BY: Guest

DATE: May 19, 2013, 4:02 a.m.

FORMAT: Python

SIZE: 724 Bytes

HITS: 2001

  1. def exercise4 (n, k):
  2. ''' Miller-Rabin procedure '''
  3. d = n-1
  4. s = 0
  5. while d % 2 == 0 and d != 1:
  6. d = d//2
  7. s = s + 1
  8. while k > 0:
  9. k = k-1;
  10. a = random.randint(2, n-2)
  11. x = a**d % n #exercise3 (a,d,n)
  12. if x == 1 or x == n-1:
  13. continue
  14. list = range(1,s)
  15. for r in list:
  16. x = x**2 % n
  17. if x == 1:
  18. return "composite"
  19. if x == n-1:
  20. continue
  21. return "composite"
  22. return "prime"

comments powered by Disqus