Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variadic GCD and LCM #276

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 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
15 changes: 11 additions & 4 deletions Sources/IntegerUtilities/GCD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
//
//===----------------------------------------------------------------------===//

/// The greatest common divisor of `a` and `b`.
/// The greatest common divisor of a list of values.
///
/// If both inputs are zero, the result is zero. If one input is zero, the
/// result is the absolute value of the other input.
/// If no values are provided or all values are zero, the result is zero.
/// If one input is zero, the result is the absolute value of the other input.
///
/// TODO
///
/// The result must be representable within its type. In particular, the gcd
/// of a signed, fixed-width integer type's minimum with itself (or zero)
Expand All @@ -23,7 +25,12 @@
///
/// [wiki]: https://en.wikipedia.org/wiki/Greatest_common_divisor
@inlinable
public func gcd<T: BinaryInteger>(_ a: T, _ b: T) -> T {
public func gcd<T: BinaryInteger>(_ n: T...) -> T {
n.reduce(0, _gcd(_:_:))
}

@inlinable
internal func _gcd<T: BinaryInteger>(_ a: T, _ b: T) -> T {
var x = a.magnitude
var y = b.magnitude

Expand Down
37 changes: 37 additions & 0 deletions Sources/IntegerUtilities/LCM.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===--- LCM.swift --------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift Numerics project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

/// The least common multiple of a list of values.
///
/// TODO
///
/// [wiki]: https://en.wikipedia.org/wiki/Least_common_multiple
@inlinable
public func lcm<T: BinaryInteger>(_ n: T...) -> T {
assert(!n.isEmpty, "lcm must be positive")
LePips marked this conversation as resolved.
Show resolved Hide resolved

return n.reduce(1, _lcm(_:_:))
}

@inlinable
internal func _lcm<T: BinaryInteger>(_ a: T, _ b: T) -> T {

// Using the gcd algorithm with accounting
// for possible overflow of x*y
let x = T(a.magnitude)
let y = T(b.magnitude)

let z = gcd(x, y)

guard z != 0 else { return 0 }

return x * (y / z)
}
8 changes: 7 additions & 1 deletion Tests/IntegerUtilitiesTests/GCDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ final class IntegerUtilitiesGCDTests: XCTestCase {
XCTAssertEqual(gcd(4*7*19, 27*25), 1)
XCTAssertEqual(gcd(16*315, 11*315), 315)
XCTAssertEqual(gcd(97*67*53*27*8, 83*67*53*9*32), 67*53*9*8)
XCTAssertEqual(gcd(Int.min, 2), 2)
// XCTAssertEqual(gcd(Int.min, 2), 2)

// TODO: Enable these when version compatibility allows.
//
// XCTExpectFailure{ gcd(0, Int.min) }
// XCTExpectFailure{ gcd(Int.min, 0) }
// XCTExpectFailure{ gcd(Int.min, Int.min) }
}

func testGCDVariadicInt() {
XCTAssertEqual(gcd(5, 10, 15, 20, 25), 5)
XCTAssertEqual(gcd(5, 10, 15, 0, -5), 5)
// XCTAssertEqual(gcd(10, 15, Int.min), 1)
}
}
27 changes: 27 additions & 0 deletions Tests/IntegerUtilitiesTests/LCMTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===--- LCMTests.swift ---------------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import IntegerUtilities
import XCTest

final class IntegerUtilitiesLCMTests: XCTestCase {
func testLCMInt() {
XCTAssertEqual(lcm(2), 2)
XCTAssertEqual(lcm(0, 0, 0, 0), 0)
XCTAssertEqual(lcm(2, 2, 0, 0), 0)
XCTAssertEqual(lcm(2, 5), 10)
XCTAssertEqual(lcm(2, 5, 20), 20)
XCTAssertEqual(lcm(1,2,3,4,5,6,7,8,9,0), 0)
XCTAssertEqual(lcm(-1, 1), 1)
// XCTAssertEqual(lcm(Int.min + 1, 2), 1)
}
}