File tree 3 files changed +8
-8
lines changed
3 files changed +8
-8
lines changed Original file line number Diff line number Diff line change 1
1
//: Playground - noun: a place where people can play
2
2
3
3
// Recursive version
4
- func gcd( a: Int , _ b: Int ) -> Int {
4
+ func gcd( _ a: Int , _ b: Int ) -> Int {
5
5
let r = a % b
6
6
if r != 0 {
7
7
return gcd ( b, r)
@@ -26,7 +26,7 @@ func gcd(m: Int, _ n: Int) -> Int {
26
26
}
27
27
*/
28
28
29
- func lcm( m: Int , _ n: Int ) -> Int {
29
+ func lcm( _ m: Int , _ n: Int ) -> Int {
30
30
return m*n / gcd( m, n)
31
31
}
32
32
Original file line number Diff line number Diff line change 1
1
/*
2
2
Euclid's algorithm for finding the greatest common divisor
3
3
*/
4
- func gcd( m: Int , _ n: Int ) -> Int {
4
+ func gcd( _ m: Int , _ n: Int ) -> Int {
5
5
var a = 0
6
6
var b = max ( m, n)
7
7
var r = min ( m, n)
@@ -16,7 +16,7 @@ func gcd(m: Int, _ n: Int) -> Int {
16
16
17
17
/*
18
18
// Recursive version
19
- func gcd(a: Int, _ b: Int) -> Int {
19
+ func gcd(_ a: Int, _ b: Int) -> Int {
20
20
let r = a % b
21
21
if r != 0 {
22
22
return gcd(b, r)
@@ -29,6 +29,6 @@ func gcd(a: Int, _ b: Int) -> Int {
29
29
/*
30
30
Returns the least common multiple of two numbers.
31
31
*/
32
- func lcm( m: Int , _ n: Int ) -> Int {
32
+ func lcm( _ m: Int , _ n: Int ) -> Int {
33
33
return m*n / gcd( m, n)
34
34
}
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ where `a % b` calculates the remainder of `a` divided by `b`.
17
17
Here is an implementation of this idea in Swift:
18
18
19
19
``` swift
20
- func gcd (a : Int , _ b : Int ) -> Int {
20
+ func gcd (_ a : Int , _ b : Int ) -> Int {
21
21
let r = a % b
22
22
if r != 0 {
23
23
return gcd (b, r)
@@ -68,7 +68,7 @@ gcd(841, 299) // 1
68
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.
69
69
70
70
``` swift
71
- func gcd (m : Int , _ n : Int ) -> Int {
71
+ func gcd (_ m : Int , _ n : Int ) -> Int {
72
72
var a = 0
73
73
var b = max (m, n)
74
74
var r = min (m, n)
@@ -101,7 +101,7 @@ We can calculate the LCM using Euclid's algorithm too:
101
101
In code:
102
102
103
103
``` swift
104
- func lcm (m : Int , _ n : Int ) -> Int {
104
+ func lcm (_ m : Int , _ n : Int ) -> Int {
105
105
return m* n / gcd (m, n)
106
106
}
107
107
```
You can’t perform that action at this time.
0 commit comments