Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions GCD/GCD.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions GCD/GCD.playground/Sources/GCD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func findEasySolution(_ m: Int, _ n: Int) -> Int? {


public enum LCMError: Error {
case divisionByZero
case nonPositive
}

/*
Expand All @@ -132,12 +132,12 @@ 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 & n != 0 else { throw LCMError.divisionByZero }
guard m > 0, n > 0 else { throw LCMError.nonPositive }
return m / gcdAlgorithm(m, n) * n
}