-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Hexagonal number sequence #5640
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d2bd767
Hexagonal number sequence
shri30yans b40553d
Update hexagonalnumbers.py
shri30yans a9bbf96
Update hexagonalnumbers.py
shri30yans 8acb035
Update hexagonalnumbers.py
shri30yans 56c8f54
Update hexagonalnumbers.py
shri30yans c5c70b0
Update and rename hexagonalnumbers.py to hexagonal_numbers.py
cclauss 0212ecc
Length must be a positive integer
cclauss 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# hexagonalnumbers.py | ||
|
||
""" | ||
A hexagonal number seqeunce is a sequence of figurate numbers | ||
where the nth hexagonal number hₙ is the number of distinct dots | ||
in a pattern of dots consisting of the outlines of regular | ||
hexagons with sides up to n dots, when the hexagons are overlaid | ||
so that they share one vertex. | ||
|
||
1. Calculates the hexagonal numbers sequence with a formula | ||
hₙ = n(2n-1) | ||
where: | ||
hₙ --> is nth element of the sequence | ||
n --> is the number of element in the sequence | ||
reference-->"Hexagonal number" Wikipedia | ||
<https://en.wikipedia.org/wiki/Hexagonal_number> | ||
|
||
2. Checks if a number is a hexagonal number with a formula | ||
n = (sqrt(8x+1)+1)/4 | ||
where: | ||
x --> is the number to check | ||
n --> is a integer if x is a hexagonal number | ||
|
||
reference-->"Hexagonal number" Wikipedia | ||
<https://en.wikipedia.org/wiki/Hexagonal_number> | ||
""" | ||
|
||
|
||
class Error(Exception): | ||
"""Base class for other exceptions""" | ||
|
||
|
||
class ValueLessThanZero(Error): | ||
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Raised when the input value is less than zero""" | ||
|
||
|
||
class ValueIsZero(Error): | ||
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Raised when the input value is zero""" | ||
|
||
|
||
def len_check(len): | ||
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len < 0: | ||
raise ValueLessThanZero | ||
elif len == 0: | ||
raise ValueIsZero | ||
|
||
|
||
def hexagonal_numbers(len: int): | ||
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
:param len: max number of elements | ||
:type len: int | ||
:return: Hexagonal numbers as a list | ||
|
||
Tests: | ||
>>> hexagonal_numbers(10) | ||
[0, 1, 6, 15, 28, 45, 66, 91, 120, 153] | ||
>>> hexagonal_numbers(5) | ||
[0, 1, 6, 15, 28] | ||
""" | ||
|
||
len_check(len) | ||
hexagonal_numbers_list = [] | ||
for n in range(len): | ||
num = n * (2 * n - 1) | ||
hexagonal_numbers_list.append(num) | ||
return hexagonal_numbers_list | ||
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def check_if_hexagonal_number(num: int): | ||
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
:param num: number to check | ||
:type num: int | ||
:return: boolean | ||
|
||
Tests: | ||
>>> check_if_hexagonal_number(120) | ||
True | ||
>>> check_if_hexagonal_number(5) | ||
False | ||
""" | ||
|
||
n = (((8 * num + 1) ** 1 / 2) + 1) / 4 | ||
isInt = True | ||
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try: | ||
int(n) | ||
except ValueError: | ||
isInt = False | ||
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return isInt | ||
|
||
|
||
if __name__ == "__main__": | ||
len = 5 | ||
hexagonal_numbers_list = hexagonal_numbers(len) | ||
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(hexagonal_numbers_list) | ||
|
||
num = 120 | ||
check = check_if_hexagonal_number(num) | ||
shri30yans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(check) |
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.