Skip to content

Commit

Permalink
chore: fix naming convention for maths functions (#209)
Browse files Browse the repository at this point in the history
* chore: added function return type

* chore: fix naming conventions for maths functions
  • Loading branch information
britneywwc authored Oct 20, 2023
1 parent 2e4d806 commit c9b3d38
Show file tree
Hide file tree
Showing 47 changed files with 199 additions and 199 deletions.
10 changes: 5 additions & 5 deletions maths/absolute_value.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @function AbsoluteValue
* @function absoluteValue
* @description Calculate the absolute value of an input number.
* @param {number} number - a numeric input value
* @return {number} - Absolute number of input number
* @see https://en.wikipedia.org/wiki/Absolute_value
* @example AbsoluteValue(-10) = 10
* @example AbsoluteValue(50) = 50
* @example AbsoluteValue(0) = 0
* @example absoluteValue(-10) = 10
* @example absoluteValue(50) = 50
* @example absoluteValue(0) = 0
*/

export const AbsoluteValue = (number: number): number => {
export const absoluteValue = (number: number): number => {
// if input number is less than 0, convert it to positive via double negation
// e.g. if n = -2, then return -(-2) = 2
return number < 0 ? -number : number;
Expand Down
8 changes: 4 additions & 4 deletions maths/aliquot_sum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @function AliquotSum
* @function aliquotSum
* @description Returns the aliquot sum of the provided number
* @summary The aliquot sum of a number n is the sum of all the proper divisors
* of n apart from n itself.
Expand All @@ -9,10 +9,10 @@
* @param {number} num The input number
* @return {number} The aliquot sum of the number
* @see [Wikipedia](https://en.wikipedia.org/wiki/Aliquot_sum)
* @example AliquotSum(18) = 21
* @example AliquotSum(15) = 9
* @example aliquotSum(18) = 21
* @example aliquotSum(15) = 9
*/
export const AliquotSum = (num: number): number => {
export const aliquotSum = (num: number): number => {
if (typeof num !== 'number') throw new TypeError('Input needs to be a number')
if (num < 0) throw new TypeError('Input cannot be negative')
if (!Number.isInteger(num)) throw new TypeError('Input cannot be a decimal')
Expand Down
8 changes: 4 additions & 4 deletions maths/armstrong_number.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @function ArmstrongNumber
* @function armstrongNumber
* @description Check if the provided number is an Armstrong number or not.
* @summary Armstrong numbers are numbers, the sum of whose digits each raised
* to the power of the number of digits is equal to the number itself.
Expand All @@ -10,10 +10,10 @@
* @return {boolean} Whether the input number is an Armstrong number
* @see [Wikipedia](https://en.wikipedia.org/wiki/Armstrong_number)
* @see [OEIS](https://oeis.org/A005188)
* @example ArmstrongNumber(370) = true
* @example ArmstrongNumber(10) = false
* @example armstrongNumber(370) = true
* @example armstrongNumber(10) = false
*/
export const ArmstrongNumber = (num: number): boolean => {
export const armstrongNumber = (num: number): boolean => {
if (typeof num !== 'number' || num <= 0) return false;

let compNum = 0
Expand Down
8 changes: 4 additions & 4 deletions maths/binary_convert.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* @function BinaryConvert
* @function binaryConvert
* @description Convert the decimal to binary.
* @param {number} num - The input integer
* @return {string} - Binary of num.
* @see [BinaryConvert](https://www.programiz.com/javascript/examples/decimal-binary)
* @example BinaryConvert(12) = 1100
* @example BinaryConvert(12 + 2) = 1110
* @example binaryConvert(12) = 1100
* @example binaryConvert(12 + 2) = 1110
*/

export const BinaryConvert = (num: number): string => {
export const binaryConvert = (num: number): string => {
let binary = ''

while (num !== 0) {
Expand Down
16 changes: 8 additions & 8 deletions maths/binomial_coefficient.ts
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;
};
8 changes: 4 additions & 4 deletions maths/digit_sum.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* @function DigitSum
* @function digitSum
* @description Calculate the sum of all digits of a natural number (number base 10).
* @param {number} num - A natural number.
* @return {number} - Sum of all digits of given natural number.
* @see https://en.wikipedia.org/wiki/Digit_sum
* @example DigitSum(12) = 3
* @example DigitSum(9045) = 18
* @example digitSum(12) = 3
* @example digitSum(9045) = 18
*/

export const DigitSum = (num: number): number => {
export const digitSum = (num: number): number => {
if (num < 0 || !Number.isInteger(num)) {
throw new Error("only natural numbers are supported");
}
Expand Down
12 changes: 6 additions & 6 deletions maths/factorial.ts
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);
};
10 changes: 5 additions & 5 deletions maths/factors.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* @function FindFactors
* @function findFactors
* @description Find all the factors of a natural number.
* @param {number} num - A natural number.
* @return {Set<number>} - A set of all the factors of given natural number.
* @see https://en.wikipedia.org/wiki/Divisor
* @example FindFactors(1) = [1]
* @example FindFactors(4) = [1,2,4]
* @example FindFactors(16) = [1,3,5,15]
* @example findFactors(1) = [1]
* @example findFactors(4) = [1,2,4]
* @example findFactors(16) = [1,3,5,15]
*/
export const FindFactors = (num: number): Set<number> => {
export const findFactors = (num: number): Set<number> => {
if (num <= 0 || !Number.isInteger(num)) {
throw new Error("Only natural numbers are supported.");
}
Expand Down
12 changes: 6 additions & 6 deletions maths/find_min.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @function FindMin
* @function findMin
* @description Find the minimum in an array of numbers.
* @param {Number[]} nums - An array of numbers.
* @return {Number} - The minimum.
* @see https://infinitbility.com/how-to-find-minimum-value-in-array-in-typescript/
* @example FindMin([1,2,3,4,5]) = 1
* @example FindMin([87,6,13,999]) = 6
* @example FindMin([0.8,0.2,0.3,0.5]) = 0.2
* @example FindMin([1,0.1,-1]) = -1
* @example findMin([1,2,3,4,5]) = 1
* @example findMin([87,6,13,999]) = 6
* @example findMin([0.8,0.2,0.3,0.5]) = 0.2
* @example findMin([1,0.1,-1]) = -1
*/
export const FindMin = (nums: number[]): number => {
export const findMin = (nums: number[]): number => {
if (nums.length === 0) {
throw new Error("array must have length of 1 or greater");
}
Expand Down
2 changes: 1 addition & 1 deletion maths/greatest_common_factor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @function GreatestCommonFactor
* @function greatestCommonFactor
* @description Determine the greatest common factor of a group of numbers.
* @param {Number[]} nums - An array of numbers.
* @return {Number} - The greatest common factor.
Expand Down
8 changes: 4 additions & 4 deletions maths/hamming_distance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @function HammingDistance
* @function hammingDistance
* @description Returns the Hamming distance between two strings of equal length
* @summary The Hamming distance between two strings of equal length is the
* number of positions at which the corresponding symbols are different. In other words,
Expand All @@ -10,9 +10,9 @@
* @param str2 One of the strings to compare to the other
* @returns {number}
* @see [Wikipedia](https://en.wikipedia.org/wiki/Hamming_distance)
* @example HammingDistance('happy', 'homie')
* @example hammingDistance('happy', 'homie')
*/
const HammingDistance = (str1: string, str2: string) => {
const hammingDistance = (str1: string, str2: string) => {
if (str1.length !== str2.length) throw new Error('Strings must of the same length.')

let dist = 0
Expand All @@ -22,4 +22,4 @@ const HammingDistance = (str1: string, str2: string) => {
return dist
}

export { HammingDistance }
export { hammingDistance }
8 changes: 4 additions & 4 deletions maths/is_divisible.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* @function IsDivisible
* @function isDivisible
* @description Checks is number is divisible by another number without remainder.
* @param {number} num1 - first number, a dividend.
* @param {number} num2 - second number, a divisor.
* @return {boolean} - true if first number can be divided by second number without a remainder.
* @example IsDivisible(10, 2) = true
* @example IsDivisible(11, 3) = false
* @example isDivisible(10, 2) = true
* @example isDivisible(11, 3) = false
*/

export const IsDivisible = (num1: number, num2: number): boolean => {
export const isDivisible = (num1: number, num2: number): boolean => {
if (num2 === 0) {
throw new Error('Cannot divide by 0');
}
Expand Down
8 changes: 4 additions & 4 deletions maths/is_even.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* @function IsEven
* @function isEven
* @description Determine whether a number is even.
* @param {Number} num - A number.
* @return {Boolean} - Whether the given number is even.
* @see https://en.wikipedia.org/wiki/Parity_(mathematics)
* @example IsEven(1) = false
* @example IsEven(2) = true
* @example isEven(1) = false
* @example isEven(2) = true
*/
export const IsEven = (num: number): boolean => {
export const isEven = (num: number): boolean => {
if (!Number.isInteger(num)) {
throw new Error("only integers can be even or odd");
}
Expand Down
8 changes: 4 additions & 4 deletions maths/is_leap_year.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @function IsLeapYear
* @function isLeapYear
* @description Checks if a year is a leap year (Gregorian calendar).
* A year is a leap year if it is divisible by 4 but not by 400 or if it is divisible by 400.
* @param {number} year - A year, natural number > 0.
* @return {boolean} - True if given year is a leap year.
* @see https://en.wikipedia.org/wiki/Leap_year#Gregorian_calendar
* @example IsLeapYear(2000) = true
* @example IsLeapYear(2001) = false
* @example isLeapYear(2000) = true
* @example isLeapYear(2001) = false
*/

export const IsLeapYear = (year: number): boolean => {
export const isLeapYear = (year: number): boolean => {
if (year <= 0 || !Number.isInteger(year)) {
throw new Error("year must be a natural number > 0");
}
Expand Down
8 changes: 4 additions & 4 deletions maths/is_odd.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* @function IsOdd
* @function isOdd
* @description Determine whether a number is odd.
* @param {Number} num - A number.
* @return {Boolean} - Whether the given number is odd.
* @see https://en.wikipedia.org/wiki/Parity_(mathematics)
* @example IsOdd(1) = true
* @example IsOdd(2) = false
* @example isOdd(1) = true
* @example isOdd(2) = false
*/
export const IsOdd = (num: number): boolean => {
export const isOdd = (num: number): boolean => {
if (!Number.isInteger(num)) {
throw new Error("only integers can be even or odd");
}
Expand Down
2 changes: 1 addition & 1 deletion maths/is_palindrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @param number The input number.
* @return {boolean} Wether the number is a Palindrome or not.
*/
export const IsPalindrome = (number: number): boolean => {
export const isPalindrome = (number: number): boolean => {
if (number < 0 || (number % 10 === 0 && number !== 0)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion maths/lowest_common_multiple.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @function LowestCommonMultiple
* @function lowestCommonMultiple
* @description Determine the lowest common multiple of a group of numbers.
* @param {Number[]} nums - An array of numbers.
* @return {Number} - The lowest common multiple.
Expand Down
10 changes: 5 additions & 5 deletions maths/number_of_digits.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @function NumberOfDigits
* @function numberOfDigits
* @description Calculate the number of digits of a natural number.
* @param {number} num - A natural number.
* @return {number} - Number of digits of given natural number.
* @see https://math.stackexchange.com/a/231745/518862
* @example NumberOfDigits(18) = 2
* @example NumberOfDigits(294568) = 6
* @example NumberOfDigits(128798319794) = 12
* @example numberOfDigits(18) = 2
* @example numberOfDigits(294568) = 6
* @example numberOfDigits(128798319794) = 12
*/

export const NumberOfDigits = (num: number): number => {
export const numberOfDigits = (num: number): number => {
if (num <= 0 || !Number.isInteger(num)) {
throw new Error("only natural numbers are supported");
}
Expand Down
2 changes: 1 addition & 1 deletion maths/perfect_square.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* @param {num} number
*/

export const PerfectSquare = (num: number) => {
export const perfectSquare = (num: number) => {
return Number.isInteger(Math.sqrt(num));
};
12 changes: 6 additions & 6 deletions maths/pronic_number.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @function PronicNumber
* @function pronicNumber
* @description Checks whether a given number is a pronic number or not
* @summary Pronic numbers, or oblong numbers as they are often referred to as,
* are numbers which are the product of two consecutive integers. That is,
Expand All @@ -9,16 +9,16 @@
* @param num The number to check for being pronic
* @returns {boolean} Whether the number is pronic or not
* @see [Wikipedia](https://en.wikipedia.org/wiki/Pronic_number)
* @example PronicNumber(20) = true
* @example PronicNumber(30) = true
* @example PronicNumber(49) = false
* @example pronicNumber(20) = true
* @example pronicNumber(30) = true
* @example pronicNumber(49) = false
*/
const PronicNumber = (n: number) => {
const pronicNumber = (n: number) => {
if (isNaN(n)) throw new Error('The input needs to be a number')
if (!Number.isInteger(n) || n < 0) throw new Error('The input needs to be a non-negative integer')
if (n === 0) return true

return !Number.isInteger(Math.sqrt(n)) && Math.floor(Math.sqrt(n)) * Math.ceil(Math.sqrt(n)) === n
}

export { PronicNumber }
export { pronicNumber }
8 changes: 4 additions & 4 deletions maths/sieve_of_eratosthenes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* @function SieveOfEratosthenes
* @function sieveOfEratosthenes
* @description Find the prime numbers between 2 and n
* @param {number} n - numbers set the limit that the algorithm needs to look to find the primes
* @return {number[]} - List of prime numbers
* @see https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes\
* @example SieveOfErastosthenes(5) = [2,3,5]
* @example SieveOfErastosthenes(10) = [2,3,5,7]
* @example sieveOfEratosthenes(5) = [2,3,5]
* @example sieveOfEratosthenes(10) = [2,3,5,7]
*/

export function SieveOfEratosthenes(n: number): number[] {
export function sieveOfEratosthenes(n: number): number[] {
if (n < 0 || !Number.isInteger(n)) {
throw new Error("Only natural numbers are supported");
}
Expand Down
Loading

0 comments on commit c9b3d38

Please sign in to comment.