A simple prime-number bot, in python. WIP
mastodon
python
fediverse
bot
mathematics
prime-numbers

millerrabin.py 881B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import random
  2. import psimp as p
  3. def MillerRabinRandom(n, k):
  4. """Carry out a Miller-Rabin test on n with k iterations, choosing random
  5. values for a.
  6. """
  7. r, d = p.trd(n-1)
  8. for iter in range(k):
  9. a = random.randint(2, n - 2)
  10. x = pow(a, d, n)
  11. if x == 1 or x == n - 1:
  12. continue
  13. compo = True
  14. for iterb in range(r - 1):
  15. x = pow(x, 2, n)
  16. if x == n - 1:
  17. compo = False
  18. break
  19. if compo:
  20. return True
  21. return False
  22. def MillerRabin(n, base):
  23. """Carry out a Miller-Rabin test on n with a specified base.
  24. """
  25. r, d = p.trd(n-1)
  26. a = base
  27. x = pow(a, d, n)
  28. if x == 1 or x == n - 1:
  29. return False
  30. for iterb in range(r - 1):
  31. x = pow(x, 2, n)
  32. if x == n - 1:
  33. return False
  34. return True