Skip to content

Commit 54d47b8

Browse files
authored
Merge pull request #219 from farzadshbfn/master
GCD
2 parents f561311 + 562c85c commit 54d47b8

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

GCD/GCD.playground/Contents.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//: Playground - noun: a place where people can play
22

33
// Recursive version
4-
func gcd(a: Int, _ b: Int) -> Int {
4+
func gcd(_ a: Int, _ b: Int) -> Int {
55
let r = a % b
66
if r != 0 {
77
return gcd(b, r)
@@ -26,7 +26,7 @@ func gcd(m: Int, _ n: Int) -> Int {
2626
}
2727
*/
2828

29-
func lcm(m: Int, _ n: Int) -> Int {
29+
func lcm(_ m: Int, _ n: Int) -> Int {
3030
return m*n / gcd(m, n)
3131
}
3232

GCD/GCD.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Euclid's algorithm for finding the greatest common divisor
33
*/
4-
func gcd(m: Int, _ n: Int) -> Int {
4+
func gcd(_ m: Int, _ n: Int) -> Int {
55
var a = 0
66
var b = max(m, n)
77
var r = min(m, n)
@@ -16,7 +16,7 @@ func gcd(m: Int, _ n: Int) -> Int {
1616

1717
/*
1818
// Recursive version
19-
func gcd(a: Int, _ b: Int) -> Int {
19+
func gcd(_ a: Int, _ b: Int) -> Int {
2020
let r = a % b
2121
if r != 0 {
2222
return gcd(b, r)
@@ -29,6 +29,6 @@ func gcd(a: Int, _ b: Int) -> Int {
2929
/*
3030
Returns the least common multiple of two numbers.
3131
*/
32-
func lcm(m: Int, _ n: Int) -> Int {
32+
func lcm(_ m: Int, _ n: Int) -> Int {
3333
return m*n / gcd(m, n)
3434
}

GCD/README.markdown

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ 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(a: Int, _ b: Int) -> Int {
20+
func gcd(_ a: Int, _ b: Int) -> Int {
2121
let r = a % b
2222
if r != 0 {
2323
return gcd(b, r)
@@ -68,7 +68,7 @@ gcd(841, 299) // 1
6868
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.
6969

7070
```swift
71-
func gcd(m: Int, _ n: Int) -> Int {
71+
func gcd(_ m: Int, _ n: Int) -> Int {
7272
var a = 0
7373
var b = max(m, n)
7474
var r = min(m, n)
@@ -101,7 +101,7 @@ We can calculate the LCM using Euclid's algorithm too:
101101
In code:
102102

103103
```swift
104-
func lcm(m: Int, _ n: Int) -> Int {
104+
func lcm(_ m: Int, _ n: Int) -> Int {
105105
return m*n / gcd(m, n)
106106
}
107107
```

0 commit comments

Comments
 (0)