forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuclideanGreatestCommonDivisorFinder.cs
More file actions
41 lines (37 loc) · 997 Bytes
/
Copy pathEuclideanGreatestCommonDivisorFinder.cs
File metadata and controls
41 lines (37 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
namespace Algorithms.Numeric.GreatestCommonDivisor
{
/// <summary>
/// TODO.
/// </summary>
public class EuclideanGreatestCommonDivisorFinder : IGreatestCommonDivisorFinder
{
/// <summary>
/// Finds greatest common divisor for numbers a and b
/// using euclidean algorithm.
/// </summary>
/// <param name="a">TODO.</param>
/// <param name="b">TODO. 2.</param>
/// <returns>Greatest common divisor.</returns>
public int FindGcd(int a, int b)
{
if (a == 0 && b == 0)
{
return int.MaxValue;
}
if (a == 0 || b == 0)
{
return a + b;
}
var aa = a;
var bb = b;
var cc = aa % bb;
while (cc != 0)
{
aa = bb;
bb = cc;
cc = aa % bb;
}
return bb;
}
}
}