Skip to content

Commit d097f69

Browse files
authored
feat: Add hexagonal numbers (#80)
1 parent 0b8f3d3 commit d097f69

File tree

3 files changed

+80
-29
lines changed

3 files changed

+80
-29
lines changed

DIRECTORY.md

+38-29
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
11

22
## Ciphers
3-
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/master/ciphers/xor_cipher.ts)
3+
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/ciphers/xor_cipher.ts)
44

55
## Data Structures
6-
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/master/data_structures/stack.ts)
6+
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack.ts)
77

88
## Dynamic Programming
9-
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/master/dynamic_programming/knapsack.ts)
9+
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts)
1010

1111
## Maths
12-
* [Absolute Value](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/absolute_value.ts)
13-
* [Aliquot Sum](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/aliquot_sum.ts)
14-
* [Armstrong Number](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/armstrong_number.ts)
15-
* [Binary Convert](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/binary_convert.ts)
16-
* [Calculate Mean](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/calculate_mean.ts)
17-
* [Degrees To Radians](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/degrees_to_radians.ts)
18-
* [Digit Sum](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/digit_sum.ts)
19-
* [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/factorial.ts)
20-
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/fibonacci.ts)
21-
* [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/find_min.ts)
22-
* [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/greatest_common_factor.ts)
23-
* [Is Divisible](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_divisible.ts)
24-
* [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_even.ts)
25-
* [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_leap_year.ts)
26-
* [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_odd.ts)
27-
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/lowest_common_multiple.ts)
28-
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/perfect_square.ts)
29-
* [Radians To Degrees](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/radians_to_degrees.ts)
30-
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/sieve_of_eratosthenes.ts)
12+
* [Absolute Value](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/absolute_value.ts)
13+
* [Aliquot Sum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/aliquot_sum.ts)
14+
* [Armstrong Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/armstrong_number.ts)
15+
* [Binary Convert](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/binary_convert.ts)
16+
* [Calculate Mean](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/calculate_mean.ts)
17+
* [Degrees To Radians](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/degrees_to_radians.ts)
18+
* [Digit Sum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/digit_sum.ts)
19+
* [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factorial.ts)
20+
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/fibonacci.ts)
21+
* [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/find_min.ts)
22+
* [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/greatest_common_factor.ts)
23+
* [Is Divisible](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_divisible.ts)
24+
* [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_even.ts)
25+
* [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_leap_year.ts)
26+
* [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_odd.ts)
27+
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts)
28+
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts)
29+
* [Radians To Degrees](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/radians_to_degrees.ts)
30+
* Series
31+
* [Hexagonal Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/hexagonal_numbers.ts)
32+
* Test
33+
* [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts)
34+
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts)
35+
36+
## Other
37+
* [Parse Nested Brackets](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/parse_nested_brackets.ts)
38+
* Test
39+
* [Parse Nested Brackets.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/test/parse_nested_brackets.test.ts)
3140

3241
## Search
33-
* [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/master/search/binary_search.ts)
34-
* [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/master/search/linear_search.ts)
42+
* [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/binary_search.ts)
43+
* [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/linear_search.ts)
3544

3645
## Sorts
37-
* [Bubble Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/bubble_sort.ts)
38-
* [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/gnome_sort.ts)
39-
* [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/insertion_sort.ts)
40-
* [Merge Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/merge_sort.ts)
41-
* [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/quick_sort.ts)
46+
* [Bubble Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/bubble_sort.ts)
47+
* [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/gnome_sort.ts)
48+
* [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/insertion_sort.ts)
49+
* [Merge Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/merge_sort.ts)
50+
* [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_sort.ts)

maths/series/hexagonal_numbers.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @function HexagonalNumbers
3+
* @description To generate the requested number of hexagonal numbers
4+
* @summary A hexagonal number, hₙ, is a figurate number which represents the number
5+
* of distinct dots in a pattern of dots consisting of the outlines of regular
6+
* hexagons with sides upto 'n' dots, when the hexagons are overlaid so that they share a common vertex
7+
*
8+
* The nth hexagonal number, hₙ, is calculated by the formula:
9+
* hₙ = n * (2n - 1)
10+
* @see [Wikipedia](https://en.wikipedia.org/wiki/Hexagonal_number)
11+
* @see [OEIS](https://oeis.org/A000384)
12+
* @param {number} n - The number of Hexagonal numbers to generate
13+
* @returns {number[]} - An array containing first 'n' hexagonal numbers
14+
* @example HexagonalNumbers(10) = [ 1, 6, 15, 28, 45, 66, 91, 120, 153, 190 ]
15+
* @example HexagonalNumbers(15) = [ 1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 276, 325, 378, 435 ]
16+
*/
17+
export const HexagonalNumbers = (n: number): number[] => {
18+
if (isNaN(n)) throw new Error('The input needs to be a number')
19+
if (!Number.isInteger(n) || n < 0) throw new Error('The input needs to be a non-negative integer')
20+
const hexagonalNumbers = []
21+
22+
for (let i = 1; i <= n; i++) {
23+
hexagonalNumbers.push(i * (2 * i - 1))
24+
}
25+
26+
return hexagonalNumbers
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { HexagonalNumbers } from "../hexagonal_numbers";
2+
3+
describe("HexagonalNumbers", () => {
4+
it("should return the first 10 hexagonal numbers", () => {
5+
expect(HexagonalNumbers(10)).toStrictEqual([1, 6, 15, 28, 45, 66, 91, 120, 153, 190]);
6+
})
7+
8+
it("should return the first 5 hexagonal numbers", () => {
9+
expect(HexagonalNumbers(5)).toStrictEqual([1, 6, 15, 28, 45])
10+
})
11+
12+
it("should return zero hexagonal numbers", () => {
13+
expect(HexagonalNumbers(0)).toStrictEqual([])
14+
})
15+
})

0 commit comments

Comments
 (0)