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

millerrabin.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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
  35. # Print pseudoprimes greater than 100 for each base, from base 1 to 15
  36. for base in range(2, 16):
  37. print("{}:".format(base))
  38. for i in range(2, 10000):
  39. if i % 2 == 0 or base % i == 0:
  40. continue
  41. if MillerRabin(i, base) == (p.hund_div(i) == 0):
  42. print(i, p.hund_div(i), MillerRabin(i, base))
  43. print()
  44. MillerRabin(28, 3)
  45. print(pow(3, 27, 28))
  46. print(pow(3, 27))
  47. print(pow(3, 27) % 28)