mirror of
https://github.com/arnaucube/math.git
synced 2026-01-16 10:51:31 +01:00
Add CRT (Chinese Remainder Theorem), gcd (using Binary Euclidean algorithm), extended gcd, inverse modulo N (using egcd)
28 lines
485 B
Python
28 lines
485 B
Python
load("number-theory.sage")
|
|
|
|
#####
|
|
# Chinese Remainder Theorem tests
|
|
a_i = [5, 3, 10]
|
|
m_i = [7, 11, 13]
|
|
M = 1001
|
|
assert crt(a_i, m_i, M) == 894
|
|
|
|
a_i = [3, 8]
|
|
m_i = [13, 17]
|
|
M = 221
|
|
assert crt(a_i, m_i, M) == 42
|
|
|
|
|
|
#####
|
|
# gcd, using Binary Euclidean algorithm tests
|
|
assert gcd(21, 12) == 3
|
|
assert gcd(1_426_668_559_730, 810_653_094_756) == 1_417_082
|
|
|
|
#####
|
|
# Extended Euclidean algorithm tests
|
|
assert egcd(7, 19) == (1, -8, 3)
|
|
|
|
#####
|
|
# Inverse modulo N tests
|
|
assert inv_mod(7, 19) == 11
|