-
-
Notifications
You must be signed in to change notification settings - Fork 420
feat: Add hexagonal numbers #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
23ec55c
feat: added hexagonal numbers 📏✏️
SpiderMath 01f9e4e
Merge branch 'TheAlgorithms:master' into add-hexagonal-nums
SpiderMath 4d02bd0
When tf did it become lowercase directories AYO
SpiderMath 614df18
Formatting filenames 4d02bd0b
actions-user 6084ad4
Update DIRECTORY.md
actions-user 050ce7e
fix to follow new filename guidelines
SpiderMath 0b3ca55
Fix imports, new rules making life hard ffs💀
SpiderMath 71206e5
Update DIRECTORY.md
actions-user 493bdf6
add empty line at the end
SpiderMath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,41 +1,50 @@ | ||
|
||
## Ciphers | ||
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/master/ciphers/xor_cipher.ts) | ||
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/ciphers/xor_cipher.ts) | ||
|
||
## Data Structures | ||
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/master/data_structures/stack.ts) | ||
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack.ts) | ||
|
||
## Dynamic Programming | ||
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/master/dynamic_programming/knapsack.ts) | ||
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts) | ||
|
||
## Maths | ||
* [Absolute Value](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/absolute_value.ts) | ||
* [Aliquot Sum](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/aliquot_sum.ts) | ||
* [Armstrong Number](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/armstrong_number.ts) | ||
* [Binary Convert](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/binary_convert.ts) | ||
* [Calculate Mean](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/calculate_mean.ts) | ||
* [Degrees To Radians](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/degrees_to_radians.ts) | ||
* [Digit Sum](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/digit_sum.ts) | ||
* [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/factorial.ts) | ||
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/fibonacci.ts) | ||
* [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/find_min.ts) | ||
* [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/greatest_common_factor.ts) | ||
* [Is Divisible](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_divisible.ts) | ||
* [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_even.ts) | ||
* [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_leap_year.ts) | ||
* [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/is_odd.ts) | ||
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/lowest_common_multiple.ts) | ||
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/perfect_square.ts) | ||
* [Radians To Degrees](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/radians_to_degrees.ts) | ||
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/master/maths/sieve_of_eratosthenes.ts) | ||
* [Absolute Value](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/absolute_value.ts) | ||
* [Aliquot Sum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/aliquot_sum.ts) | ||
* [Armstrong Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/armstrong_number.ts) | ||
* [Binary Convert](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/binary_convert.ts) | ||
* [Calculate Mean](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/calculate_mean.ts) | ||
* [Degrees To Radians](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/degrees_to_radians.ts) | ||
* [Digit Sum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/digit_sum.ts) | ||
* [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factorial.ts) | ||
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/fibonacci.ts) | ||
* [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/find_min.ts) | ||
* [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/greatest_common_factor.ts) | ||
* [Is Divisible](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_divisible.ts) | ||
* [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_even.ts) | ||
* [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_leap_year.ts) | ||
* [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_odd.ts) | ||
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts) | ||
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts) | ||
* [Radians To Degrees](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/radians_to_degrees.ts) | ||
* Series | ||
* [Hexagonal Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/hexagonal_numbers.ts) | ||
* Test | ||
* [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts) | ||
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts) | ||
|
||
## Other | ||
* [Parse Nested Brackets](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/parse_nested_brackets.ts) | ||
* Test | ||
* [Parse Nested Brackets.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/test/parse_nested_brackets.test.ts) | ||
|
||
## Search | ||
* [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/master/search/binary_search.ts) | ||
* [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/master/search/linear_search.ts) | ||
* [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/binary_search.ts) | ||
* [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/linear_search.ts) | ||
|
||
## Sorts | ||
* [Bubble Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/bubble_sort.ts) | ||
* [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/gnome_sort.ts) | ||
* [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/insertion_sort.ts) | ||
* [Merge Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/merge_sort.ts) | ||
* [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/master/sorts/quick_sort.ts) | ||
* [Bubble Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/bubble_sort.ts) | ||
* [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/gnome_sort.ts) | ||
* [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/insertion_sort.ts) | ||
* [Merge Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/merge_sort.ts) | ||
* [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_sort.ts) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @function HexagonalNumbers | ||
* @description To generate the requested number of hexagonal numbers | ||
* @summary A hexagonal number, hₙ, is a figurate number which represents the number | ||
* of distinct dots in a pattern of dots consisting of the outlines of regular | ||
* hexagons with sides upto 'n' dots, when the hexagons are overlaid so that they share a common vertex | ||
* | ||
* The nth hexagonal number, hₙ, is calculated by the formula: | ||
* hₙ = n * (2n - 1) | ||
* @see [Wikipedia](https://en.wikipedia.org/wiki/Hexagonal_number) | ||
* @see [OEIS](https://oeis.org/A000384) | ||
* @param {number} n - The number of Hexagonal numbers to generate | ||
* @returns {number[]} - An array containing first 'n' hexagonal numbers | ||
* @example HexagonalNumbers(10) = [ 1, 6, 15, 28, 45, 66, 91, 120, 153, 190 ] | ||
* @example HexagonalNumbers(15) = [ 1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 276, 325, 378, 435 ] | ||
*/ | ||
export const HexagonalNumbers = (n: number): 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') | ||
const hexagonalNumbers = [] | ||
|
||
for (let i = 1; i <= n; i++) { | ||
hexagonalNumbers.push(i * (2 * i - 1)) | ||
} | ||
|
||
return hexagonalNumbers | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { HexagonalNumbers } from "../hexagonal_numbers"; | ||
|
||
describe("HexagonalNumbers", () => { | ||
it("should return the first 10 hexagonal numbers", () => { | ||
expect(HexagonalNumbers(10)).toStrictEqual([1, 6, 15, 28, 45, 66, 91, 120, 153, 190]); | ||
}) | ||
|
||
it("should return the first 5 hexagonal numbers", () => { | ||
expect(HexagonalNumbers(5)).toStrictEqual([1, 6, 15, 28, 45]) | ||
}) | ||
|
||
it("should return zero hexagonal numbers", () => { | ||
expect(HexagonalNumbers(0)).toStrictEqual([]) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.