2020-11-29 17:46:26 +00:00
|
|
|
from typing import Tuple
|
2019-10-06 18:52:04 +00:00
|
|
|
|
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
def diophantine(a: int, b: int, c: int) -> Tuple[float, float]:
|
2019-10-06 18:52:04 +00:00
|
|
|
"""
|
2020-11-29 17:46:26 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
|
|
|
|
|
2019-10-06 18:52:04 +00:00
|
|
|
>>> diophantine(10,6,14)
|
|
|
|
(-7.0, 14.0)
|
|
|
|
|
|
|
|
>>> diophantine(391,299,-69)
|
|
|
|
(9.0, -12.0)
|
|
|
|
|
|
|
|
But above equation has one more solution i.e., x = -4, y = 5.
|
|
|
|
That's why we need diophantine all solution function.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-10-18 06:13:58 +00:00
|
|
|
assert (
|
|
|
|
c % greatest_common_divisor(a, b) == 0
|
|
|
|
) # greatest_common_divisor(a,b) function implemented below
|
2019-10-06 18:52:04 +00:00
|
|
|
(d, x, y) = extended_gcd(a, b) # extended_gcd(a,b) function implemented below
|
|
|
|
r = c / d
|
|
|
|
return (r * x, r * y)
|
|
|
|
|
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
def diophantine_all_soln(a: int, b: int, c: int, n: int = 2) -> None:
|
|
|
|
"""
|
|
|
|
Lemma : if n|ab and gcd(a,n) = 1, then n|b.
|
2019-10-06 18:52:04 +00:00
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
Finding All solutions of Diophantine Equations:
|
2019-10-06 18:52:04 +00:00
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
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.
|
2019-10-06 18:52:04 +00:00
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
n is the number of solution you want, n = 2 by default
|
2019-10-18 06:13:58 +00:00
|
|
|
|
2019-10-06 18:52:04 +00:00
|
|
|
>>> diophantine_all_soln(10, 6, 14)
|
|
|
|
-7.0 14.0
|
|
|
|
-4.0 9.0
|
|
|
|
|
|
|
|
>>> diophantine_all_soln(10, 6, 14, 4)
|
|
|
|
-7.0 14.0
|
|
|
|
-4.0 9.0
|
|
|
|
-1.0 4.0
|
|
|
|
2.0 -1.0
|
|
|
|
|
|
|
|
>>> diophantine_all_soln(391, 299, -69, n = 4)
|
|
|
|
9.0 -12.0
|
|
|
|
22.0 -29.0
|
|
|
|
35.0 -46.0
|
|
|
|
48.0 -63.0
|
|
|
|
|
|
|
|
"""
|
|
|
|
(x0, y0) = diophantine(a, b, c) # Initial value
|
|
|
|
d = greatest_common_divisor(a, b)
|
|
|
|
p = a // d
|
|
|
|
q = b // d
|
|
|
|
|
|
|
|
for i in range(n):
|
|
|
|
x = x0 + i * q
|
|
|
|
y = y0 - i * p
|
|
|
|
print(x, y)
|
|
|
|
|
|
|
|
|
2020-11-29 16:20:54 +00:00
|
|
|
def greatest_common_divisor(a: int, b: int) -> int:
|
2019-10-06 18:52:04 +00:00
|
|
|
"""
|
2020-11-29 17:46:26 +00:00
|
|
|
Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
|
|
|
|
|
|
|
|
Euclid's Algorithm
|
|
|
|
|
2019-10-06 18:52:04 +00:00
|
|
|
>>> greatest_common_divisor(7,5)
|
|
|
|
1
|
|
|
|
|
2020-06-16 08:09:19 +00:00
|
|
|
Note : In number theory, two integers a and b are said to be relatively prime,
|
|
|
|
mutually prime, or co-prime if the only positive integer (factor) that
|
|
|
|
divides both of them is 1 i.e., gcd(a,b) = 1.
|
2019-10-06 18:52:04 +00:00
|
|
|
|
|
|
|
>>> greatest_common_divisor(121, 11)
|
|
|
|
11
|
|
|
|
|
|
|
|
"""
|
|
|
|
if a < b:
|
|
|
|
a, b = b, a
|
|
|
|
|
|
|
|
while a % b != 0:
|
|
|
|
a, b = b, a % b
|
|
|
|
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
def extended_gcd(a: int, b: int) -> Tuple[int, int, int]:
|
2019-10-06 18:52:04 +00:00
|
|
|
"""
|
2020-11-29 17:46:26 +00:00
|
|
|
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)
|
|
|
|
|
2019-10-06 18:52:04 +00:00
|
|
|
>>> extended_gcd(10, 6)
|
|
|
|
(2, -1, 2)
|
|
|
|
|
|
|
|
>>> extended_gcd(7, 5)
|
|
|
|
(1, -2, 3)
|
|
|
|
|
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2019-10-18 06:13:58 +00:00
|
|
|
if __name__ == "__main__":
|
2020-05-19 10:56:16 +00:00
|
|
|
from doctest import testmod
|
|
|
|
|
2019-10-18 06:13:58 +00:00
|
|
|
testmod(name="diophantine", verbose=True)
|
|
|
|
testmod(name="diophantine_all_soln", verbose=True)
|
|
|
|
testmod(name="extended_gcd", verbose=True)
|
|
|
|
testmod(name="greatest_common_divisor", verbose=True)
|