-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: fix naming convention for maths functions (#209)
* chore: added function return type * chore: fix naming conventions for maths functions
- Loading branch information
1 parent
2e4d806
commit c9b3d38
Showing
47 changed files
with
199 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import { Factorial } from "./factorial"; | ||
import { factorial } from "./factorial"; | ||
/** | ||
* @function BinomialCoefficient | ||
* @function binomialCoefficient | ||
* @description Calculate the binomial coefficient (n choose k) of two input numbers. | ||
* @param {number} n - the total number of items | ||
* @param {number} k - the number of items to be chosen | ||
* @return {number} - Binomial coefficient (n choose k) | ||
* @see https://en.wikipedia.org/wiki/Binomial_coefficient | ||
* @example BinomialCoefficient(5, 2) = 10 | ||
* @example BinomialCoefficient(10, 3) = 120 | ||
* @example BinomialCoefficient(6, 0) = 1 | ||
* @example binomialCoefficient(5, 2) = 10 | ||
* @example binomialCoefficient(10, 3) = 120 | ||
* @example binomialCoefficient(6, 0) = 1 | ||
*/ | ||
|
||
export const BinomialCoefficient = (n: number, k: number): number => { | ||
export const binomialCoefficient = (n: number, k: number): number => { | ||
// Check if k is larger than n or negative | ||
if (k > n || k < 0) { | ||
return 0; | ||
} | ||
|
||
// Calculate the binomial coefficient using the implemented factorial | ||
const numerator = Factorial(n); | ||
const denominator = Factorial(k) * Factorial(n - k); | ||
const numerator = factorial(n); | ||
const denominator = factorial(k) * factorial(n - k); | ||
return numerator / denominator; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
/** | ||
* @function Factorial | ||
* @function factorial | ||
* @description Calculate the factorial of a natural number. | ||
* @param {number} num - A natural number. | ||
* @return {number} - The factorial. | ||
* @see https://en.wikipedia.org/wiki/Factorial | ||
* @example Factorial(0) = 1 | ||
* @example Factorial(3) = 6 | ||
* @see https://en.wikipedia.org/wiki/factorial | ||
* @example factorial(0) = 1 | ||
* @example factorial(3) = 6 | ||
*/ | ||
export const Factorial = (num: number): number => { | ||
export const factorial = (num: number): number => { | ||
if (num < 0 || !Number.isInteger(num)) { | ||
throw new Error("only natural numbers are supported"); | ||
} | ||
|
||
return num === 0 ? 1 : num * Factorial(num - 1); | ||
return num === 0 ? 1 : num * factorial(num - 1); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.