Skip to content

Commit 516f9ec

Browse files
committed
Add GCD and LCM
1 parent 706d545 commit 516f9ec

File tree

6 files changed

+165
-1
lines changed

6 files changed

+165
-1
lines changed

GCD/GCD.playground/Contents.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//: Playground - noun: a place where people can play
2+
3+
func gcd(m: Int, _ n: Int) -> Int {
4+
var a = 0
5+
var b = max(m, n)
6+
var r = min(m, n)
7+
8+
while r != 0 {
9+
a = b
10+
b = r
11+
r = a % b
12+
}
13+
return b
14+
}
15+
16+
func lcm(m: Int, _ n: Int) -> Int {
17+
return m*n / gcd(m, n)
18+
}
19+
20+
gcd(39, 52) // 13
21+
gcd(36, 228) // 12
22+
gcd(3819, 51357) // 57
23+
gcd(841, 299) // 1
24+
25+
lcm(2, 3) // 6
26+
lcm(10, 8) // 40
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

GCD/GCD.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Euclid's algorithm for finding the greatest common divisor
3+
*/
4+
func gcd(m: Int, _ n: Int) -> Int {
5+
var a = 0
6+
var b = max(m, n)
7+
var r = min(m, n)
8+
9+
while r != 0 {
10+
a = b
11+
b = r
12+
r = a % b
13+
}
14+
return b
15+
}
16+
17+
/*
18+
Returns the least common multiple of two numbers.
19+
*/
20+
func lcm(m: Int, _ n: Int) -> Int {
21+
return m*n / gcd(m, n)
22+
}

GCD/README.markdown

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Greatest Common Divisor
2+
3+
The *greatest common divisor* (or Greatest Common Factor) of two numbers `a` and `b` is the largest positive integer that divides both `a` and `b` without a remainder.
4+
5+
For example, `gcd(39, 52) = 13` because 13 divides 39 (`39/13 = 3`) as well as 52 (`52/13 = 4`). But there is no larger number than 13 that divides them both.
6+
7+
You've probably had to learn about this in school at some point. :-)
8+
9+
The laborious way to find the GCD of two numbers is to first figure out the factors of both numbers, then take the greatest number they have in common. The problem is that factoring numbers is quite difficult, especially when they get larger. (On the plus side, that difficulty is also what keeps your online payments secure.)
10+
11+
There is a smarter way to calculate the GCD: Euclid's algorithm. The big idea here is that,
12+
13+
gcd(a, b) = gcd(b, a % b)
14+
15+
where `a % b` calculates the remainder of `a` divided by `b`.
16+
17+
Here is an implementation of this idea in Swift:
18+
19+
```swift
20+
func gcd(m: Int, _ n: Int) -> Int {
21+
var a = 0
22+
var b = max(m, n)
23+
var r = min(m, n)
24+
25+
while r != 0 {
26+
a = b
27+
b = r
28+
r = a % b
29+
}
30+
return b
31+
}
32+
```
33+
34+
Put it in a playground and try it out with these examples:
35+
36+
```swift
37+
gcd(39, 52) // 13
38+
gcd(36, 228) // 12
39+
gcd(3819, 51357) // 57
40+
```
41+
42+
Let's step through the third example:
43+
44+
gcd(3819, 51357)
45+
46+
It's convenient to have the larger number first, so we swap them. That's what the `max()` and `min()` are for at the top of the function. We now have:
47+
48+
gcd(51357, 3819)
49+
50+
According to Euclid's rule, this is equivalent to:
51+
52+
gcd(3819, 51357 % 3819) = gcd(3819, 1710)
53+
54+
because the remainder of `51357 % 3819` is `1710`. If you work out this division you get `51357 = (13 * 3819) + 1710` but we only care about the remainder part.
55+
56+
So `gcd(51357, 3819)` is the same as `gcd(3819, 1710)`. That's useful because we can keep simplifying:
57+
58+
gcd(3819, 1710) = gcd(1710, 3819 % 1710) =
59+
gcd(1710, 399) = gcd(399, 1710 % 399) =
60+
gcd(399, 114) = gcd(114, 399 % 114) =
61+
gcd(114, 57) = gcd(57, 114 % 57) =
62+
gcd(57, 0)
63+
64+
And now can't divide any further. The remainder of `114 / 57` is zero because `114 = 57 * 2` exactly. That means we've found the answer:
65+
66+
gcd(3819, 51357) = gcd(57, 0) = 57
67+
68+
So in each step of Euclid's algorithm the numbers become smaller and at some point it ends when one of them becomes zero.
69+
70+
By the way, it's also possible that two numbers have a GCD of 1. They are said to be *relatively prime*. This happens when there is no number that divides them both, for example:
71+
72+
```swift
73+
gcd(841, 299) // 1
74+
```
75+
76+
## Least Common Multiple
77+
78+
An idea related to the GCD is the *least common multiple* or LCM.
79+
80+
The least common multiple of two numbers `a` and `b` is the smallest positive integer that is a multiple of both. In other words, the LCM is evenly divisible by `a` and `b`.
81+
82+
For example: `lcm(2, 3) = 6` because 6 can be divided by 2 and also by 3.
83+
84+
We can calculate the LCM using Euclid's algorithm too:
85+
86+
a * b
87+
lcm(a, b) = ---------
88+
gcd(a, b)
89+
90+
In code:
91+
92+
```swift
93+
func lcm(m: Int, _ n: Int) -> Int {
94+
return m*n / gcd(m, n)
95+
}
96+
```
97+
98+
And to try it out in a playground:
99+
100+
```swift
101+
lcm(10, 8) // 40
102+
```
103+
104+
You probably won't need to use the GCD or LCM in any real-world problems, but it's cool to play around with this ancient algorithm. It was first described by Euclid in his [Elements](http://publicdomainreview.org/collections/the-first-six-books-of-the-elements-of-euclid-1847/) around 300 BC. Rumor has it that he discovered this algorithm while he was hacking on his Commodore 64.
105+
106+
*Written for Swift Algorithm Club by Matthijs Hollemans*

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Bad sorting algorithms (don't use these!):
8080

8181
### Mathematics
8282

83-
- Greatest Common Divisor (GCD)
83+
- [Greatest Common Divisor (GCD)](GCD/). Special bonus: the least common multiple.
8484
- Statistics
8585
- Combinatorics (permutations etc)
8686

0 commit comments

Comments
 (0)