From 6cf9675a5683d0e97357891bdd21f73ca6367622 Mon Sep 17 00:00:00 2001 From: Arthur Guiot Date: Sat, 23 Sep 2023 16:34:55 -0700 Subject: [PATCH] Fixed combinations --- Sources/Euler/NumberTheory/combinations.swift | 4 ++-- Tests/EulerTests/TablesTest.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/Euler/NumberTheory/combinations.swift b/Sources/Euler/NumberTheory/combinations.swift index 0855ba2..740125a 100644 --- a/Sources/Euler/NumberTheory/combinations.swift +++ b/Sources/Euler/NumberTheory/combinations.swift @@ -31,7 +31,7 @@ public func permutationsWithRepitition(_ n: Int, _ k: Int) -> BigInt { public func combinationsWithRepetitions(_ n: Int, _ k: Int) throws -> BigInt { // (n + k - 1)! / (k! * (n - 1)!) guard k != 0 && n != 1 else { throw EulerError.DivisionByZero } - return try factorial(BigInt(n + k - 1)) / factorial(BigInt(k)) * factorial(BigInt(n - 1)) + return try factorial(BigInt(n + k - 1)) / (factorial(BigInt(k)) * factorial(BigInt(n - 1))) } /// Combinations: `$\frac{n!}{k! * (n - k)!}$`. @@ -42,5 +42,5 @@ public func combinationsWithRepetitions(_ n: Int, _ k: Int) throws -> BigInt { public func combinations(_ n: Int, _ k: Int) throws -> BigInt { // n! / (k! * (n - k)!) guard n - k != 0 else { throw EulerError.DivisionByZero } - return try factorial(BigInt(n)) / factorial(BigInt(k)) * factorial(BigInt(n - k)) + return try factorial(BigInt(n)) / (factorial(BigInt(k)) * factorial(BigInt(n - k))) } diff --git a/Tests/EulerTests/TablesTest.swift b/Tests/EulerTests/TablesTest.swift index 42ba090..4409674 100644 --- a/Tests/EulerTests/TablesTest.swift +++ b/Tests/EulerTests/TablesTest.swift @@ -11,7 +11,7 @@ class TablesTests: XCTestCase { func testCommon() { let t = Tables() XCTAssertEqual(t.ABS(-2), 2) - XCTAssertEqual(try t.COMBIN(5, k: 3), 40) + XCTAssertEqual(try t.COMBIN(5, k: 3), 10) XCTAssertEqual(t.DECIMAL("ff", 16), 255) XCTAssert(t.DEGREES(pi / 2).nearlyEquals(90)) XCTAssertEqual(try t.FACTDOUBLE(6).scientificDescription, "2.6012×10¹⁷⁴⁶")