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 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
12 changes: 8 additions & 4 deletions Sources/IntegerUtilities/GCD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
//
//===----------------------------------------------------------------------===//

/// The greatest common divisor of `a` and `b`.
/// The greatest common divisor of passed 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.
/// 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 +22,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>(_ a: T, _ n: T...) -> T {
n.reduce(a, _gcd(_:_:))
}

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

Expand Down
35 changes: 35 additions & 0 deletions Sources/IntegerUtilities/LCM.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===--- 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 passed values.
///
/// TODO
///
/// [wiki]: https://en.wikipedia.org/wiki/Least_common_multiple
@inlinable
public func lcm<T: BinaryInteger>(_ a: T, _ n: T...) -> T {
n.reduce(a, _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)
}
5 changes: 4 additions & 1 deletion Tests/IntegerUtilitiesTests/GCDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import XCTest

final class IntegerUtilitiesGCDTests: XCTestCase {
func testGCDInt() {
XCTAssertEqual(gcd(0), 0)
XCTAssertEqual(gcd(5, 10, 15, 20, 25), 5)
XCTAssertEqual(gcd(5, 10, 15, 0, -5), 5)
XCTAssertEqual(gcd(0, 0), 0)
XCTAssertEqual(gcd(0, 1), 1)
XCTAssertEqual(gcd(1, 0), 1)
Expand All @@ -33,7 +36,7 @@ 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.
//
Expand Down
29 changes: 29 additions & 0 deletions Tests/IntegerUtilitiesTests/LCMTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===--- 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(0), 0)
XCTAssertEqual(lcm(2), 2)
XCTAssertEqual(lcm(2, 5), 10)
XCTAssertEqual(lcm(0, 0), 0)
XCTAssertEqual(lcm(-1, 1), 1)
XCTAssertEqual(lcm(2, 2, 0), 0)
XCTAssertEqual(lcm(2, 5, 20), 20)
XCTAssertEqual(lcm(1, 2, 3, 4, 5, 6, 7, 8, 9), 2520)
XCTAssertEqual(lcm(1, 2, 3, 4, 5, 6, 7, 8, 9, 0), 0)
// XCTAssertEqual(lcm(Int.min + 1, 2), 1)
}
}