-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added algorithm for creating Hamming numbers series in Python #4992
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
b4c3b97
Added algorithm for creating Hamming numbers series in Python
und1n3 d7bb365
Changed to f-string format.
und1n3 df11ba1
Added modifications
und1n3 64faa42
Update and rename hamming.py to hamming_numbers.py
poyea 8554af1
Update hamming_numbers.py
poyea efa3df0
Update hamming_numbers.py
poyea 54b06a5
Rename maths/series/hamming_numbers.py to maths/hamming_numbers.py
poyea 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,60 @@ | ||
""" | ||
Run the doctests with the following command: | ||
python3 -m doctest -v hamming_numbers.py | ||
or | ||
python -m doctest -v hamming_numbers.py | ||
For manual testing run: | ||
python3 hamming_numbers.py | ||
|
||
""" | ||
|
||
|
||
def hamming(n_element: int) -> list: | ||
""" | ||
A Hamming number is a positive integer of the form 2^i*3^j*5^k, for some | ||
non-negative integers i, j, and k. | ||
More info at: https://en.wikipedia.org/wiki/Regular_number . | ||
poyea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This function creates an ordered list of n length as requested, and afterwards | ||
returns the last value of the list. It must be given a positive integer. | ||
|
||
:param n_element: The number of elements on the list | ||
:return: The nth element of the list | ||
|
||
Examples: | ||
poyea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> hamming(5) | ||
[1, 2, 3, 4, 5] | ||
>>> hamming(10) | ||
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12] | ||
>>> hamming(15) | ||
[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24] | ||
""" | ||
n_element = int(n_element) | ||
if n_element < 1: | ||
my_error= ValueError("a should be a positive number") | ||
raise my_error | ||
|
||
hamming_list = [1] | ||
i, j, k = (0, 0, 0) | ||
index = 1 | ||
while index < n_element: | ||
while hamming_list[i] * 2 <= hamming_list[-1]: | ||
i += 1 | ||
while hamming_list[j] * 3 <= hamming_list[-1]: | ||
j += 1 | ||
while hamming_list[k] * 5 <= hamming_list[-1]: | ||
k += 1 | ||
hamming_list.append( | ||
min(hamming_list[i] * 2, hamming_list[j] * 3, hamming_list[k] * 5) | ||
) | ||
index += 1 | ||
return hamming_list | ||
|
||
|
||
if __name__ == "__main__": | ||
n = input("Enter the last number (nth term) of the Hamming Number Series: ") | ||
print("Formula of Hamming Number Series => 2^i * 3^j * 5^k") | ||
hamming_numbers = hamming(n) | ||
print("-----------------------------------------------------") | ||
print("The list with nth numbers is: {}".format(hamming_numbers)) | ||
und1n3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print("-----------------------------------------------------") |
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.