File tree Expand file tree Collapse file tree 3 files changed +8
-8
lines changed Expand file tree Collapse file tree 3 files changed +8
-8
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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}
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`.
1717Here 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
6868Here 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:
101101In 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```
You can’t perform that action at this time.
0 commit comments