Skip to content

Diophantine Equation #1236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions data_structures/hashing/number_theory/chinese_remainder_theorem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Chinese Remainder Theorem:

# If GCD(a,b) = 1, then for any remainder ra modulo a and any remainder rb modulo b there exists integer n,
# such that n = ra (mod a) and n = ra(mod b). If n1 and n2 are two such integers, then n1=n2(mod ab)

# Algorithm :

# 1. Use extended euclid algorithm to find x,y such that a*x + b*y = 1
# 2. Take n = ra*by + rb*ax


# Extended Euclid
def ExtendedEuclid(a, b):
if b == 0:
return (1, 0)
(x, y) = ExtendedEuclid(b, a % b)
k = a // b
return (y, x - k * y)


# Uses ExtendedEuclid to find inverses
def ChineseRemainderTheorem(n1, r1, n2, r2):
(x, y) = ExtendedEuclid(n1, n2)
m = n1 * n2
n = r2 * x * n1 + r1 * y * n2
return ((n % m + m) % m)


# ----------SAME SOLUTION USING InvertModulo instead ExtendedEuclid----------------

# This function find the inverses of a i.e., a^(-1)
def InvertModulo(a, n):
(b, x) = ExtendedEuclid(a, n)
if b < 0:
b = (b % n + n) % n
return b


# Same a above using InvertingModulo
def ChineseRemainderTheorem2(n1, r1, n2, r2):
x, y = InvertModulo(n1, n2), InvertModulo(n2, n1)
m = n1 * n2
n = r2 * x * n1 + r1 * y * n2
return (n % m + m) % m
64 changes: 64 additions & 0 deletions data_structures/hashing/number_theory/diophantine_equation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Diophantine Equation : Given integers a,b,c ( at least one of a and b != 0), the diophantine equation
# a*x + b*y = c has a solution (where x and y are integers) iff gcd(a,b) divides c.


def diophantine(a, b, c):
assert c % gcd(a, b) == 0 # gcd(a,b) function implemented below

(d, x, y) = extended_gcd(a, b) # extended_gcd(a,b) function implemented below
r = c / d
return (r * x, r * y)


# Lemma : if n|ab and gcd(a,n) = 1, then n|b.

# Finding All solutions of Diophantine Equations:

# Theorem : Let gcd(a,b) = d, a = d*p, b = d*q. If (x0,y0) is a solution of Diophantine Equation a*x + b*y = c.
# a*x0 + b*y0 = c, then all the solutions have the form a(x0 + t*q) + b(y0 - t*p) = c, where t is an arbitrary integer.

# n is the number of solution you want, n = 2 by default

def diophantine_all_soln(a, b, c, n=2):
(x0, y0) = diophantine(a, b, c)
d = gcd(a, b)
p = a // d
q = b // d

for i in range(n):
x = x0 + i * q
y = y0 - i * p
print(x, y)


# Euclid's Lemma : d divides a and b, if and only if d divides a-b and b

# Euclid's Algorithm

def gcd(a, b):
if a < b:
a, b = b, a

while a % b != 0:
a, b = b, a % b

return b


# Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers x and y, then d = gcd(a,b)


def extended_gcd(a, b):
assert a >= 0 and b >= 0

if b == 0:
d, x, y = a, 1, 0
else:
(d, p, q) = extended_gcd(b, a % b)
x = q
y = p - q * (a // b)

assert a % d == 0 and b % d == 0
assert d == a * x + b * y

return (d, x, y)