Skip to content

Commit

Permalink
Issue 87: Stack efficient Euclidean method
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanGrange committed Apr 25, 2020
1 parent eb2cd79 commit 46513c9
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions Algorithms/Numeric/GreatestCommonDivisor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@ public static class GreatestCommonDivisor
/// </summary>
public static int FindGCDEuclidean(int a, int b)
{
int t = 0;

a = Math.Abs(a);
b = Math.Abs(b);

if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;

return Euclidean(a, b);
}

private static int Euclidean(int a, int b)
{
if (b == 0)
return a;

return Euclidean(b, a % b);
while (a % b != 0) {
t = b;
b = a % b;
a = t;
}
return b;
}

/// <summary>
Expand Down

0 comments on commit 46513c9

Please sign in to comment.