-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.test.ts
More file actions
106 lines (89 loc) · 4.11 KB
/
factorial.test.ts
File metadata and controls
106 lines (89 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { factorial, combination, permutation } from "../src/factorial";
describe("factorial", () => {
test("should calculate basic factorials", () => {
expect(factorial("0")).toBe("1");
expect(factorial("1")).toBe("1");
expect(factorial("2")).toBe("2");
expect(factorial("3")).toBe("6");
expect(factorial("4")).toBe("24");
expect(factorial("5")).toBe("120");
});
test("should calculate larger factorials", () => {
expect(factorial("6")).toBe("720");
expect(factorial("7")).toBe("5040");
expect(factorial("10")).toBe("3628800");
});
test("should handle very large factorials", () => {
// 20! = 2432902008176640000
expect(factorial("20")).toBe("2432902008176640000");
});
test("should throw error for negative numbers", () => {
expect(() => factorial("-1")).toThrow("Factorial is not defined for negative numbers");
expect(() => factorial("-5")).toThrow("Factorial is not defined for negative numbers");
});
test("should throw error for non-integers", () => {
expect(() => factorial("5.5")).toThrow("Factorial is only defined for integers");
expect(() => factorial("3.14")).toThrow("Factorial is only defined for integers");
});
});
describe("combination", () => {
test("should calculate basic combinations", () => {
expect(combination("5", "2")).toBe("10"); // C(5,2) = 10
expect(combination("5", "3")).toBe("10"); // C(5,3) = 10
expect(combination("4", "2")).toBe("6"); // C(4,2) = 6
expect(combination("6", "3")).toBe("20"); // C(6,3) = 20
});
test("should handle edge cases", () => {
expect(combination("5", "0")).toBe("1"); // C(n,0) = 1
expect(combination("5", "5")).toBe("1"); // C(n,n) = 1
expect(combination("5", "6")).toBe("0"); // C(n,r) = 0 when r > n
});
test("should handle large combinations", () => {
expect(combination("10", "3")).toBe("120"); // C(10,3) = 120
expect(combination("15", "5")).toBe("3003"); // C(15,5) = 3003
});
test("should use symmetry optimization", () => {
// C(10,8) should equal C(10,2) = 45
expect(combination("10", "8")).toBe("45");
expect(combination("10", "2")).toBe("45");
});
test("should throw error for negative numbers", () => {
expect(() => combination("-5", "2")).toThrow("Combination is not defined for negative numbers");
expect(() => combination("5", "-2")).toThrow("Combination is not defined for negative numbers");
});
test("should throw error for non-integers", () => {
expect(() => combination("5.5", "2")).toThrow("Combination requires integer arguments");
expect(() => combination("5", "2.5")).toThrow("Combination requires integer arguments");
});
});
describe("permutation", () => {
test("should calculate basic permutations", () => {
expect(permutation("5", "2")).toBe("20"); // P(5,2) = 20
expect(permutation("5", "3")).toBe("60"); // P(5,3) = 60
expect(permutation("4", "2")).toBe("12"); // P(4,2) = 12
expect(permutation("6", "3")).toBe("120"); // P(6,3) = 120
});
test("should handle edge cases", () => {
expect(permutation("5", "0")).toBe("1"); // P(n,0) = 1
expect(permutation("5", "5")).toBe("120"); // P(5,5) = 5! = 120
expect(permutation("5", "6")).toBe("0"); // P(n,r) = 0 when r > n
});
test("should handle large permutations", () => {
expect(permutation("10", "3")).toBe("720"); // P(10,3) = 720
expect(permutation("8", "4")).toBe("1680"); // P(8,4) = 1680
});
test("should throw error for negative numbers", () => {
expect(() => permutation("-5", "2")).toThrow("Permutation is not defined for negative numbers");
expect(() => permutation("5", "-2")).toThrow("Permutation is not defined for negative numbers");
});
test("should throw error for non-integers", () => {
expect(() => permutation("5.5", "2")).toThrow("Permutation requires integer arguments");
expect(() => permutation("5", "2.5")).toThrow("Permutation requires integer arguments");
});
test("should relate to combinations correctly", () => {
// P(n,r) = C(n,r) × r!
// P(5,3) = C(5,3) × 3! = 10 × 6 = 60
expect(permutation("5", "3")).toBe("60");
expect(combination("5", "3")).toBe("10");
});
});