Skip to content

Commit 897712d

Browse files
authored
Merge pull request aalhour#132 from RyanGrange/issue87
Issue 87: Stack efficient Euclidean method
2 parents eb2cd79 + 46513c9 commit 897712d

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

Algorithms/Numeric/GreatestCommonDivisor.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,22 @@ public static class GreatestCommonDivisor
99
/// </summary>
1010
public static int FindGCDEuclidean(int a, int b)
1111
{
12+
int t = 0;
13+
1214
a = Math.Abs(a);
1315
b = Math.Abs(b);
1416

1517
if (a == 0)
1618
return b;
1719
if (b == 0)
1820
return a;
19-
if (a == b)
20-
return a;
21-
22-
return Euclidean(a, b);
23-
}
24-
25-
private static int Euclidean(int a, int b)
26-
{
27-
if (b == 0)
28-
return a;
2921

30-
return Euclidean(b, a % b);
22+
while (a % b != 0) {
23+
t = b;
24+
b = a % b;
25+
a = t;
26+
}
27+
return b;
3128
}
3229

3330
/// <summary>

0 commit comments

Comments
 (0)