Skip to content

Commit

Permalink
[NBKCoreKit] Greatest common divisor.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Oct 26, 2023
1 parent fb5565a commit a095025
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
13 changes: 6 additions & 7 deletions Sources/NBKCoreKit/Private/NBK+GreatestCommonDivisor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ extension NBK {
@inlinable public static func greatestCommonDivisorByBinaryAlgorithm<T>(
of lhs: T, and rhs: T) -> T.Magnitude where T: NBKBinaryInteger {
//=--------------------------------------=
if lhs.isZero { return rhs.magnitude }
if rhs.isZero { return lhs.magnitude }
//=--------------------------------------=
var lhs: T.Magnitude = lhs.magnitude
var rhs: T.Magnitude = rhs.magnitude
//=--------------------------------------=
var lhs = lhs.magnitude as T.Magnitude
if rhs.isZero { return lhs }
var rhs = rhs.magnitude as T.Magnitude
if lhs.isZero { return rhs }

let lhsShift: Int = lhs.trailingZeroBitCount
let rhsShift: Int = rhs.trailingZeroBitCount
//=--------------------------------------=
Expand All @@ -48,7 +47,7 @@ extension NBK {
}
}

lhs >>= Swift.min(lhsShift, rhsShift)
lhs <<= Swift.min(lhsShift, rhsShift)
return lhs as T.Magnitude
}
}
58 changes: 48 additions & 10 deletions Tests/NBKCoreKitTests/Private/NBK+GreatestCommonDivisor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,54 @@ final class NBKTestsOnGreatestCommonDivisor: XCTestCase {
// MARK: Tests
//=------------------------------------------------------------------------=

func testSmallValues() {
NBKAssertGreatestCommonDivisor(2 as Int, 3 * 5 * 7 as Int, 1 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 as Int, 3 * 5 * 7 as Int, 3 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 as Int, 3 * 5 * 7 as Int, 3 * 5 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 * 7 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 3 * 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 7 * 11 as Int, 3 * 5 * 7 as Int, 7 as UInt)
NBKAssertGreatestCommonDivisor( 11 as Int, 3 * 5 * 7 as Int, 1 as UInt)
func testPowersOf2() {
NBKAssertGreatestCommonDivisor( 2, 16, 2)
NBKAssertGreatestCommonDivisor( 4, 16, 4)
NBKAssertGreatestCommonDivisor( 8, 16, 8)
NBKAssertGreatestCommonDivisor(16, 16, 16)
}

func testSmallPrimeProductsRepeated() {
NBKAssertGreatestCommonDivisor( 2 * 3 * 5 as Int, 16 * 81 as Int, 2 * 3 as UInt)
NBKAssertGreatestCommonDivisor( 4 * 9 * 25 as Int, 16 * 81 as Int, 4 * 9 as UInt)
NBKAssertGreatestCommonDivisor( 8 * 27 * 125 as Int, 16 * 81 as Int, 8 * 27 as UInt)
NBKAssertGreatestCommonDivisor(16 * 81 * 625 as Int, 16 * 81 as Int, 16 * 81 as UInt)
}

func testSmallPrimeProductsEvenEven() {
NBKAssertGreatestCommonDivisor(2 as Int, 2 * 5 * 11 as Int, 2 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 as Int, 2 * 5 * 11 as Int, 2 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 as Int, 2 * 5 * 11 as Int, 2 * 5 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 * 7 as Int, 2 * 5 * 11 as Int, 2 * 5 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 * 7 * 11 as Int, 2 * 5 * 11 as Int, 2 * 5 * 11 as UInt)
NBKAssertGreatestCommonDivisor( 3 * 5 * 7 * 11 as Int, 2 * 5 * 11 as Int, 5 * 11 as UInt)
NBKAssertGreatestCommonDivisor( 5 * 7 * 11 as Int, 2 * 5 * 11 as Int, 5 * 11 as UInt)
NBKAssertGreatestCommonDivisor( 7 * 11 as Int, 2 * 5 * 11 as Int, 11 as UInt)
NBKAssertGreatestCommonDivisor( 11 as Int, 2 * 5 * 11 as Int, 11 as UInt)
}

func testSmallPrimeProductsEvenOdd() {
NBKAssertGreatestCommonDivisor(2 as Int, 3 * 5 * 7 as Int, 1 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 as Int, 3 * 5 * 7 as Int, 3 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 as Int, 3 * 5 * 7 as Int, 3 * 5 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 * 7 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor(2 * 3 * 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 3 * 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 7 * 11 as Int, 3 * 5 * 7 as Int, 7 as UInt)
NBKAssertGreatestCommonDivisor( 11 as Int, 3 * 5 * 7 as Int, 1 as UInt)
}

func testSmallPrimeProductsOddOdd() {
NBKAssertGreatestCommonDivisor(1 as Int, 3 * 5 * 7 as Int, 1 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 as Int, 3 * 5 * 7 as Int, 3 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 as Int, 3 * 5 * 7 as Int, 3 * 5 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 * 7 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor(1 * 3 * 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 3 * 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 3 * 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 5 * 7 * 11 as Int, 3 * 5 * 7 as Int, 5 * 7 as UInt)
NBKAssertGreatestCommonDivisor( 7 * 11 as Int, 3 * 5 * 7 as Int, 7 as UInt)
NBKAssertGreatestCommonDivisor( 11 as Int, 3 * 5 * 7 as Int, 1 as UInt)
}

func testEverythingDividesZero() {
Expand Down

0 comments on commit a095025

Please sign in to comment.