euclidean-test.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from test import test
  2. from euclidean import *
  3. test(1, gcd(7, 9))
  4. test(2, gcd(8, 18))
  5. test(-12, gcd(-12, 24))
  6. test(12, gcd(12, -24)) # gcd is only unique up to multiplication by a unit, and so sometimes we'll get negatives.
  7. test(38, gcd(4864, 3458))
  8. test((32, -45, 38), extendedEuclideanAlgorithm(4864, 3458))
  9. test((-45, 32, 38), extendedEuclideanAlgorithm(3458, 4864))
  10. from modp import *
  11. Mod2 = IntegersModP(2)
  12. test(Mod2(1), gcd(Mod2(1), Mod2(0)))
  13. test(Mod2(1), gcd(Mod2(1), Mod2(1)))
  14. test(Mod2(0), gcd(Mod2(2), Mod2(2)))
  15. Mod7 = IntegersModP(7)
  16. test(Mod7(6), gcd(Mod7(6), Mod7(14)))
  17. test(Mod7(2), gcd(Mod7(6), Mod7(9)))
  18. ModHuge = IntegersModP(9923)
  19. test(ModHuge(38), gcd(ModHuge(4864), ModHuge(3458)))
  20. test((ModHuge(32), ModHuge(-45), ModHuge(38)),
  21. extendedEuclideanAlgorithm(ModHuge(4864), ModHuge(3458)))
  22. from polynomial import *
  23. p = polynomialsOver(Mod7).factory
  24. test(p([-1, 1]), gcd(p([-1,0,1]), p([-1,0,0,1])))
  25. f = p([-1,0,1])
  26. g = p([-1,0,0,1])
  27. test((p([0,-1]), p([1]), p([-1, 1])), extendedEuclideanAlgorithm(f, g))
  28. test(p([-1,1]), f * p([0,-1]) + g * p([1]))
  29. p = polynomialsOver(Mod2).factory
  30. f = p([1,0,0,0,1,1,1,0,1,1,1]) # x^10 + x^9 + x^8 + x^6 + x^5 + x^4 + 1
  31. g = p([1,0,1,1,0,1,1,0,0,1]) # x^9 + x^6 + x^5 + x^3 + x^1 + 1
  32. theGcd = p([1,1,0,1]) # x^3 + x + 1
  33. x = p([0,0,0,0,1]) # x^4
  34. y = p([1,1,1,1,1,1]) # x^5 + x^4 + x^3 + x^2 + x + 1
  35. test((x, y, theGcd), extendedEuclideanAlgorithm(f, g))