Update Combinations.java and add test case (#3184)

This commit is contained in:
Ankush Banik 2022-07-08 17:01:09 +05:30 committed by GitHub
parent 826b612d0d
commit f7bd7682ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 17 deletions

View File

@ -5,23 +5,6 @@ package com.thealgorithms.maths;
*/
public class Combinations {
public static void main(String[] args) {
assert combinations(1, 1) == 1;
assert combinations(10, 5) == 252;
assert combinations(6, 3) == 20;
assert combinations(20, 5) == 15504;
// Since, 200 is a big number its factorial will go beyond limits of long even when 200C5 can be saved in a long
// variable. So below will fail
// assert combinations(200, 5) == 2535650040l;
assert combinationsOptimized(100, 0) == 1;
assert combinationsOptimized(1, 1) == 1;
assert combinationsOptimized(10, 5) == 252;
assert combinationsOptimized(6, 3) == 20;
assert combinationsOptimized(20, 5) == 15504;
assert combinationsOptimized(200, 5) == 2535650040l;
}
/**
* Calculate of factorial
*

View File

@ -0,0 +1,30 @@
package com.thealgorithms.maths;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CombinationsTest {
@Test
void testCombination() {
assertEquals(1, Combinations.combinations(1, 1));
assertEquals(252, Combinations.combinations(10, 5));
assertEquals(20, Combinations.combinations(6, 3));
assertEquals(15504, Combinations.combinations(20, 5));
}
@Test
void testCombinationOptimised() {
assertEquals(100, Combinations.combinationsOptimized(100, 1));
assertEquals(1, Combinations.combinationsOptimized(1, 1));
assertEquals(252, Combinations.combinationsOptimized(10, 5));
assertEquals(20, Combinations.combinationsOptimized(6, 3));
assertEquals(15504, Combinations.combinationsOptimized(20, 5));
assertEquals(2535650040L, Combinations.combinationsOptimized(200, 5));
}
}