Skip to content

Commit

Permalink
factorial and combination
Browse files Browse the repository at this point in the history
  • Loading branch information
mzusin committed Apr 23, 2024
1 parent 6c7192f commit cc4778d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 18 deletions.
4 changes: 2 additions & 2 deletions dist/mz-math.esm.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/mz-math.esm.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/mz-math.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/mz-math.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/mz-math.node.cjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/mz-math.node.cjs.map

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions src/main/combinatorics/combinatorics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export const permutationsWithRepetition = (n: number, r: number) => {
if (n < 0 || r < 0) {
throw new Error('Both n and r should be non-negative integers.');
}

if(r > n) {
throw new Error('r cannot be greater than n.');
}

if (!Number.isInteger(n) || !Number.isInteger(r)) {
throw new Error('Both n and r should be integers.');
}
Expand Down Expand Up @@ -58,6 +63,11 @@ export const permutationsWithoutRepetition = (n: number, r: number) => {
if (n < 0 || r < 0) {
throw new Error('Both n and r should be non-negative integers.');
}

if(r > n) {
throw new Error('r cannot be greater than n.');
}

if (!Number.isInteger(n) || !Number.isInteger(r)) {
throw new Error('Both n and r should be integers.');
}
Expand Down Expand Up @@ -91,6 +101,11 @@ export const combinationsWithoutRepetition = (n: number, r: number) : number =>
if (n < 0 || r < 0) {
throw new Error('Both n and r should be non-negative integers.');
}

if(r > n) {
throw new Error('r cannot be greater than n.');
}

if (!Number.isInteger(n) || !Number.isInteger(r)) {
throw new Error('Both n and r should be integers.');
}
Expand Down Expand Up @@ -152,6 +167,11 @@ export const combinationsWithRepetition = (n: number, r: number) : number => {
if (n < 0 || r < 0) {
throw new Error('Both n and r should be non-negative integers.');
}

if(r > n) {
throw new Error('r cannot be greater than n.');
}

if (!Number.isInteger(n) || !Number.isInteger(r)) {
throw new Error('Both n and r should be integers.');
}
Expand Down
27 changes: 21 additions & 6 deletions test/combinatorics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@ describe('Combinatorics', () => {
});

it('handles edge cases with zero correctly', () => {
expect(permutationsWithRepetition(0, 5)).toBe(0); // 0^5 should be 0
expect(permutationsWithRepetition(5, 0)).toBe(1); // 5^0 should be 1
});

it('throws an error if r is greater than n', () => {
expect(() => permutationsWithRepetition(0, 5)).toThrow('r cannot be greater than n.');
expect(() => permutationsWithRepetition(2, 5)).toThrow('r cannot be greater than n.');
});
});

describe('permutationsWithoutRepetition()', () => {
it('calculates correct permutations for valid inputs', () => {
expect(permutationsWithoutRepetition(16, 3)).toBe(3360);
});

it('throws an error if r is greater than n', () => {
expect(() => permutationsWithoutRepetition(0, 5)).toThrow('r cannot be greater than n.');
expect(() => permutationsWithoutRepetition(2, 5)).toThrow('r cannot be greater than n.');
});
});

describe('combinationsWithoutRepetition()', () => {
Expand Down Expand Up @@ -65,9 +74,11 @@ describe('Combinatorics', () => {
expect(() => combinationsWithoutRepetition(5, -1)).toThrow('Both n and r should be non-negative integers.');
});

/*it('throws an error if r is greater than n', () => {
expect(() => combinationsWithoutRepetition(3, 5)).toThrow('n and r must be non-negative integers, and r cannot be greater than n.');
});*/
it('throws an error if r is greater than n', () => {
expect(() => combinationsWithoutRepetition(3, 5)).toThrow('r cannot be greater than n.');
expect(() => combinationsWithoutRepetition(2, 5)).toThrow('r cannot be greater than n.');
expect(() => combinationsWithoutRepetition(0, 5)).toThrow('r cannot be greater than n.');
});

it('throws an error if n or r are not integers', () => {
expect(() => combinationsWithoutRepetition(5.5, 3)).toThrow('Both n and r should be integers.');
Expand All @@ -89,7 +100,6 @@ describe('Combinatorics', () => {

it('calculates combinations correctly when n is 1', () => {
expect(combinationsWithRepetition(1, 1)).toBe(1);
expect(combinationsWithRepetition(1, 5)).toBe(1); // C(1+5-1, 5) = C(5, 5) = 1
});

it('throws an error if n or r are negative', () => {
Expand All @@ -103,7 +113,12 @@ describe('Combinatorics', () => {

it('handles cases where n is 0', () => {
expect(combinationsWithRepetition(0, 0)).toBe(1); // C(0+0-1, 0) = C(-1, 0) = 1 (special handled)
expect(combinationsWithRepetition(0, 1)).toBe(1); // C(0+1-1, 1) = C(0, 1) = 1 (special handled)
});

it('throws an error if r is greater than n', () => {
expect(() => combinationsWithRepetition(3, 5)).toThrow('r cannot be greater than n.');
expect(() => combinationsWithRepetition(2, 5)).toThrow('r cannot be greater than n.');
expect(() => combinationsWithRepetition(0, 5)).toThrow('r cannot be greater than n.');
});
});
});

0 comments on commit cc4778d

Please sign in to comment.