Skip to content

Commit 408997c

Browse files
committed
Merge branch 'development'
2 parents c527ea4 + 2932351 commit 408997c

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

Algorithms/Algorithms.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<Compile Include="Graphs\DijkstraShortestPaths.cs" />
3838
<Compile Include="Numeric\BinomialCoefficients.cs" />
3939
<Compile Include="Numeric\CatalanNumbers.cs" />
40+
<Compile Include="Numeric\GreatestCommonDivisor.cs" />
4041
<Compile Include="Properties\AssemblyInfo.cs" />
4142
<Compile Include="Sorting\BinarySearchTreeSorter.cs" />
4243
<Compile Include="Sorting\BubbleSorter.cs" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/***
2+
* Euclidean Algorithm to find the greatest common divisor of two numbers.
3+
*
4+
*/
5+
6+
7+
namespace Algorithms.Numeric
8+
{
9+
public static class GreatestCommonDivisor
10+
{
11+
/// <summary>
12+
/// Finds and returns the greatest common divisor of two numbers
13+
/// </summary>
14+
public static uint FindGCD(uint a, uint b)
15+
{
16+
if (a == 0)
17+
return b;
18+
else if (b == 0)
19+
return a;
20+
21+
uint _a = a, _b = b;
22+
23+
uint q = _a / _b;
24+
uint r = _a % _b;
25+
26+
while(r != 0)
27+
{
28+
_a = _b;
29+
_b = r;
30+
r = _a % _b;
31+
}
32+
33+
return _b;
34+
}
35+
36+
/// <summary>
37+
/// Determines given two numbers are relatively prime
38+
/// </summary>
39+
public static bool IsRelativelyPrime(uint a, uint b)
40+
{
41+
return FindGCD(a, b) == 1;
42+
}
43+
}
44+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
3+
using Algorithms.Numeric;
4+
5+
6+
namespace C_Sharp_Algorithms.AlgorithmsTests
7+
{
8+
public static class GreatestCommonDivisorTest
9+
{
10+
11+
public static void DoTest()
12+
{
13+
uint[,] testNumbers = new uint[8, 2]
14+
{
15+
{0,0 },
16+
{0,5 },
17+
{2,4 },
18+
{3,10 },
19+
{13,17 },
20+
{12,20 },
21+
{22,11 },
22+
{33,22 }
23+
};
24+
25+
for (int i = 0; i < testNumbers.GetLength(0); i++)
26+
{
27+
uint gcd = GreatestCommonDivisor.FindGCD(testNumbers[i, 0], testNumbers[i, 1]);
28+
29+
if (Assert(testNumbers[i,0], testNumbers[i,1], gcd))
30+
{
31+
Console.WriteLine("{0,5}-{1,-5}: with gcd: {2,-5} : Success", testNumbers[i, 0], testNumbers[i, 1], gcd);
32+
}
33+
else
34+
{
35+
Console.WriteLine("{0,5}-{1,-5}: with gcd: {2,-5} : Error", testNumbers[i, 0], testNumbers[i, 1], gcd);
36+
}
37+
}
38+
}
39+
40+
private static bool Assert(uint a, uint b, uint gcdCandidate)
41+
{
42+
if (a == 0)
43+
return b == gcdCandidate;
44+
else if (b == 0)
45+
return a == gcdCandidate;
46+
47+
if (a % gcdCandidate != 0 || b % gcdCandidate != 0)
48+
return false;
49+
50+
//Suppose that gcdCandidate is not gcd.
51+
//With this assumption, if gcdCandidate divides both a and b then it must divide the real gcd
52+
//Namely, there should be a positive integer
53+
uint q;
54+
//such that gcd = q*gcdCandidate
55+
56+
q = 2;
57+
58+
uint _gcd = gcdCandidate * q;
59+
60+
while(_gcd < a && _gcd < b)
61+
{
62+
if (a % _gcd == 0 && b % _gcd == 0)
63+
return false;
64+
65+
_gcd *= ++q;
66+
}
67+
68+
return a > b ?
69+
((a % b == 0) && (b == gcdCandidate)) || ((a % b != 0) && (b != gcdCandidate)) :
70+
((b % a == 0) && (a == gcdCandidate)) || ((b % a != 0) && (a != gcdCandidate));
71+
}
72+
73+
}
74+
}

MainProgram/MainProgram.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<Compile Include="AlgorithmsTests\GraphsCyclesDetectorTests.cs" />
4949
<Compile Include="AlgorithmsTests\GraphsDijkstraShortestPathsTest.cs" />
5050
<Compile Include="AlgorithmsTests\GraphsTopologicalSorterTest.cs" />
51+
<Compile Include="AlgorithmsTests\GreatestCommonDivisorTest.cs" />
5152
<Compile Include="AlgorithmsTests\HeapSorterTest.cs" />
5253
<Compile Include="AlgorithmsTests\LSDRadixSorterTest.cs" />
5354
<Compile Include="AlgorithmsTests\StringEditDistanceTest.cs" />

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Note: The projects where tested on Xamarin Studio (MonoDevelop) on OSX 10, and V
119119

120120
#### Numeric:
121121
* [Catalan Numbers](Algorithms/Numeric/CatalanNumbers.cs).
122+
* [Greatest Common Divisor](Algorithms/Numeric/GreatestCommonDivisor.cs)
122123

123124
#### Visualization:
124125
* [Tree Drawer](DataStructures/Trees/TreeDrawer.cs).

0 commit comments

Comments
 (0)