Skip to content

Commit

Permalink
✨ adds algorithm to find diophantine equation solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
iagorrr committed Feb 10, 2024
1 parent 28ec615 commit 684b648
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions algorithms/math/find-solution-diophantine-equation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ll gcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll x1, y1;
ll d = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}

bool find_any_solution(ll a, ll b, ll c, ll &x0, ll &y0) {
auto g = gcd(a < 0 ? -a : a, b < 0 ? -b : b, x0, y0);
if (c % g) {
return false;
}

x0 *= c / g;
y0 *= c / g;
if (a < 0) x0 = -x0;
if (b < 0) y0 = -y0;
return true;
}
5 changes: 5 additions & 0 deletions algorithms/math/find-solution-diophantine-equation.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
\subsection{Linear Diophantine Equation: Find any solution}

Given $a$ $b$, $c$ finds the solution to the equation $ax + by = c$, the result will be stored in the reference variables $x0$ and $y0$

time: $O(\log{min(a,b)})$

0 comments on commit 684b648

Please sign in to comment.