From 05be19d8a8c0fad4d73fcfcb418e1d7b5b9d1d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geir-K=C3=A5re=20S=2E=20W=C3=A6rp?= Date: Wed, 21 Oct 2020 19:40:35 +0200 Subject: [PATCH 1/3] Added failing test. --- GCD/GCD.playground/Contents.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/GCD/GCD.playground/Contents.swift b/GCD/GCD.playground/Contents.swift index bb8d49055..7da3ccc26 100644 --- a/GCD/GCD.playground/Contents.swift +++ b/GCD/GCD.playground/Contents.swift @@ -16,6 +16,7 @@ gcd(841, 299, using: gcdBinaryRecursiveStein) // 1 do { try lcm(2, 3) // 6 try lcm(10, 8, using: gcdRecursiveEuklid) // 40 + try lcm(3, 4) // 12 } catch { dump(error) } From 4a914ebb3f7756da613151a5c4d05ebfc12ca033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geir-K=C3=A5re=20S=2E=20W=C3=A6rp?= Date: Wed, 21 Oct 2020 19:41:22 +0200 Subject: [PATCH 2/3] Fixed incorrect check for non-negative numbers. --- GCD/GCD.playground/Sources/GCD.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GCD/GCD.playground/Sources/GCD.swift b/GCD/GCD.playground/Sources/GCD.swift index 4eb0c7621..bea4c7707 100644 --- a/GCD/GCD.playground/Sources/GCD.swift +++ b/GCD/GCD.playground/Sources/GCD.swift @@ -121,7 +121,7 @@ func findEasySolution(_ m: Int, _ n: Int) -> Int? { public enum LCMError: Error { - case divisionByZero + case nonPositive } /* @@ -138,6 +138,6 @@ public enum LCMError: Error { an unsigned integer */ public func lcm(_ m: Int, _ n: Int, using gcdAlgorithm: (Int, Int) -> (Int) = gcdIterativeEuklid) throws -> Int { - guard m & n != 0 else { throw LCMError.divisionByZero } + guard m > 0, n > 0 else { throw LCMError.nonPositive } return m / gcdAlgorithm(m, n) * n } From 8cef3150f76bc9e73cbea3918d62cd42ac83a006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geir-K=C3=A5re=20S=2E=20W=C3=A6rp?= Date: Wed, 21 Oct 2020 19:43:09 +0200 Subject: [PATCH 3/3] Updated documentation. --- GCD/GCD.playground/Sources/GCD.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GCD/GCD.playground/Sources/GCD.swift b/GCD/GCD.playground/Sources/GCD.swift index bea4c7707..fffbfc7b2 100644 --- a/GCD/GCD.playground/Sources/GCD.swift +++ b/GCD/GCD.playground/Sources/GCD.swift @@ -132,10 +132,10 @@ public enum LCMError: Error { - Parameter using: The used gcd algorithm to calculate the lcm. If nothing provided, the Iterative Euclidean algorithm is used. - - Throws: Can throw a `divisionByZero` error if one of the given + - Throws: Can throw a `nonPositive` error if one or both of the given attributes turns out to be zero or less. - Returns: The least common multiplier of the two attributes as - an unsigned integer + a signed integer */ public func lcm(_ m: Int, _ n: Int, using gcdAlgorithm: (Int, Int) -> (Int) = gcdIterativeEuklid) throws -> Int { guard m > 0, n > 0 else { throw LCMError.nonPositive }