Update Combinations.java and add test case (#3184)
This commit is contained in:
parent
826b612d0d
commit
f7bd7682ba
@ -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
|
||||
*
|
||||
|
30
src/test/java/com/thealgorithms/maths/CombinationsTest.java
Normal file
30
src/test/java/com/thealgorithms/maths/CombinationsTest.java
Normal 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));
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user