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 -
(a: int, b: int, c: int, n: int = 2)
| 31 | |
| 32 | |
| 33 | def diophantine_all_soln(a: int, b: int, c: int, n: int = 2) -> None: |
| 34 | """ |
| 35 | Lemma : if n|ab and gcd(a,n) = 1, then n|b. |
| 36 | |
| 37 | Finding All solutions of Diophantine Equations: |
| 38 | |
| 39 | Theorem : Let gcd(a,b) = d, a = d*p, b = d*q. If (x0,y0) is a solution of |
| 40 | Diophantine Equation a*x + b*y = c. a*x0 + b*y0 = c, then all the |
| 41 | solutions have the form a(x0 + t*q) + b(y0 - t*p) = c, |
| 42 | where t is an arbitrary integer. |
| 43 | |
| 44 | n is the number of solution you want, n = 2 by default |
| 45 | |
| 46 | >>> diophantine_all_soln(10, 6, 14) |
| 47 | -7.0 14.0 |
| 48 | -4.0 9.0 |
| 49 | |
| 50 | >>> diophantine_all_soln(10, 6, 14, 4) |
| 51 | -7.0 14.0 |
| 52 | -4.0 9.0 |
| 53 | -1.0 4.0 |
| 54 | 2.0 -1.0 |
| 55 | |
| 56 | >>> diophantine_all_soln(391, 299, -69, n = 4) |
| 57 | 9.0 -12.0 |
| 58 | 22.0 -29.0 |
| 59 | 35.0 -46.0 |
| 60 | 48.0 -63.0 |
| 61 | |
| 62 | """ |
| 63 | (x0, y0) = diophantine(a, b, c) # Initial value |
| 64 | d = greatest_common_divisor(a, b) |
| 65 | p = a // d |
| 66 | q = b // d |
| 67 | |
| 68 | for i in range(n): |
| 69 | x = x0 + i * q |
| 70 | y = y0 - i * p |
| 71 | print(x, y) |
| 72 | |
| 73 | |
| 74 | def extended_gcd(a: int, b: int) -> tuple[int, int, int]: |
nothing calls this directly
no test coverage detected