Skip to content

Commit 2d24e5d

Browse files
author
Chris Pilcher
committed
Merge branch 'master' of https://github.com/hollance/swift-algorithm-club into breadth-first-search
2 parents a1f6cac + 6943444 commit 2d24e5d

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

GCD/GCD.playground/Contents.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
//: Playground - noun: a place where people can play
22

3+
// Recursive version
4+
func gcd(a: Int, _ b: Int) -> Int {
5+
let r = a % b
6+
if r != 0 {
7+
return gcd(b, r)
8+
} else {
9+
return b
10+
}
11+
}
12+
13+
/*
14+
// Iterative version
315
func gcd(m: Int, _ n: Int) -> Int {
416
var a = 0
517
var b = max(m, n)
@@ -12,14 +24,15 @@ func gcd(m: Int, _ n: Int) -> Int {
1224
}
1325
return b
1426
}
27+
*/
1528

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

20-
gcd(39, 52) // 13
21-
gcd(36, 228) // 12
22-
gcd(3819, 51357) // 57
33+
gcd(52, 39) // 13
34+
gcd(228, 36) // 12
35+
gcd(51357, 3819) // 57
2336
gcd(841, 299) // 1
2437

2538
lcm(2, 3) // 6

GCD/GCD.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ func gcd(m: Int, _ n: Int) -> Int {
1414
return b
1515
}
1616

17+
/*
18+
// Recursive version
19+
func gcd(a: Int, _ b: Int) -> Int {
20+
let r = a % b
21+
if r != 0 {
22+
return gcd(b, r)
23+
} else {
24+
return b
25+
}
26+
}
27+
*/
28+
1729
/*
1830
Returns the least common multiple of two numbers.
1931
*/

GCD/README.markdown

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,26 @@ where `a % b` calculates the remainder of `a` divided by `b`.
1717
Here is an implementation of this idea in Swift:
1818

1919
```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
20+
func gcd(a: Int, _ b: Int) -> Int {
21+
let r = a % b
22+
if r != 0 {
23+
return gcd(b, r)
24+
} else {
25+
return b
2926
}
30-
return b
3127
}
3228
```
3329

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

3632
```swift
37-
gcd(39, 52) // 13
38-
gcd(36, 228) // 12
39-
gcd(3819, 51357) // 57
33+
gcd(52, 39) // 13
34+
gcd(228, 36) // 12
35+
gcd(51357, 3819) // 57
4036
```
4137

4238
Let's step through the third example:
4339

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-
4840
gcd(51357, 3819)
4941

5042
According to Euclid's rule, this is equivalent to:
@@ -73,6 +65,23 @@ By the way, it's also possible that two numbers have a GCD of 1. They are said t
7365
gcd(841, 299) // 1
7466
```
7567

68+
Here is a slightly different implementation of Euclid's algorithm. Unlike the first version this doesn't use recursion but only a basic `while` loop. The `max()` and `min()` at the top of the function make sure we always divide the larger number by the smaller one.
69+
70+
```swift
71+
func gcd(m: Int, _ n: Int) -> Int {
72+
var a = 0
73+
var b = max(m, n)
74+
var r = min(m, n)
75+
76+
while r != 0 {
77+
a = b
78+
b = r
79+
r = a % b
80+
}
81+
return b
82+
}
83+
```
84+
7685
## Least Common Multiple
7786

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

0 commit comments

Comments
 (0)