Skip to content

Commit

Permalink
Add GCD and LCM
Browse files Browse the repository at this point in the history
hollance committed Jan 30, 2016

Verified

This commit was signed with the committer’s verified signature.
colingm Colin Maxfield
1 parent 706d545 commit 516f9ec
Showing 6 changed files with 165 additions and 1 deletion.
26 changes: 26 additions & 0 deletions GCD/GCD.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//: Playground - noun: a place where people can play

func gcd(m: Int, _ n: Int) -> Int {
var a = 0
var b = max(m, n)
var r = min(m, n)

while r != 0 {
a = b
b = r
r = a % b
}
return b
}

func lcm(m: Int, _ n: Int) -> Int {
return m*n / gcd(m, n)
}

gcd(39, 52) // 13
gcd(36, 228) // 12
gcd(3819, 51357) // 57
gcd(841, 299) // 1

lcm(2, 3) // 6
lcm(10, 8) // 40
4 changes: 4 additions & 0 deletions GCD/GCD.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='osx'>
<timeline fileName='timeline.xctimeline'/>
</playground>
6 changes: 6 additions & 0 deletions GCD/GCD.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
22 changes: 22 additions & 0 deletions GCD/GCD.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Euclid's algorithm for finding the greatest common divisor
*/
func gcd(m: Int, _ n: Int) -> Int {
var a = 0
var b = max(m, n)
var r = min(m, n)

while r != 0 {
a = b
b = r
r = a % b
}
return b
}

/*
Returns the least common multiple of two numbers.
*/
func lcm(m: Int, _ n: Int) -> Int {
return m*n / gcd(m, n)
}
106 changes: 106 additions & 0 deletions GCD/README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Greatest Common Divisor

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.

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.

You've probably had to learn about this in school at some point. :-)

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.)

There is a smarter way to calculate the GCD: Euclid's algorithm. The big idea here is that,

gcd(a, b) = gcd(b, a % b)

where `a % b` calculates the remainder of `a` divided by `b`.

Here is an implementation of this idea in Swift:

```swift
func gcd(m: Int, _ n: Int) -> Int {
var a = 0
var b = max(m, n)
var r = min(m, n)

while r != 0 {
a = b
b = r
r = a % b
}
return b
}
```

Put it in a playground and try it out with these examples:

```swift
gcd(39, 52) // 13
gcd(36, 228) // 12
gcd(3819, 51357) // 57
```

Let's step through the third example:

gcd(3819, 51357)

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:

gcd(51357, 3819)

According to Euclid's rule, this is equivalent to:

gcd(3819, 51357 % 3819) = gcd(3819, 1710)

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.

So `gcd(51357, 3819)` is the same as `gcd(3819, 1710)`. That's useful because we can keep simplifying:

gcd(3819, 1710) = gcd(1710, 3819 % 1710) =
gcd(1710, 399) = gcd(399, 1710 % 399) =
gcd(399, 114) = gcd(114, 399 % 114) =
gcd(114, 57) = gcd(57, 114 % 57) =
gcd(57, 0)

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:

gcd(3819, 51357) = gcd(57, 0) = 57

So in each step of Euclid's algorithm the numbers become smaller and at some point it ends when one of them becomes zero.

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:

```swift
gcd(841, 299) // 1
```

## Least Common Multiple

An idea related to the GCD is the *least common multiple* or LCM.

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`.

For example: `lcm(2, 3) = 6` because 6 can be divided by 2 and also by 3.

We can calculate the LCM using Euclid's algorithm too:

a * b
lcm(a, b) = ---------
gcd(a, b)

In code:

```swift
func lcm(m: Int, _ n: Int) -> Int {
return m*n / gcd(m, n)
}
```

And to try it out in a playground:

```swift
lcm(10, 8) // 40
```

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.

*Written for Swift Algorithm Club by Matthijs Hollemans*
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ Bad sorting algorithms (don't use these!):

### Mathematics

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

0 comments on commit 516f9ec

Please sign in to comment.