mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 09:10:16 +00:00
4d0c830d2c
* ci(pre-commit): Add ``flake8-builtins`` additional dependency to ``pre-commit`` (#7104) * refactor: Fix ``flake8-builtins`` (#7104) * fix(lru_cache): Fix naming conventions in docstrings (#7104) * ci(pre-commit): Order additional dependencies alphabetically (#7104) * fix(lfu_cache): Correct function name in docstring (#7104) * Update strings/snake_case_to_camel_pascal_case.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/stacks/next_greater_element.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update digital_image_processing/index_calculation.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update graphs/prim.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update hashes/djb2.py Co-authored-by: Christian Clauss <cclauss@me.com> * refactor: Rename `_builtin` to `builtin_` ( #7104) * fix: Rename all instances (#7104) * refactor: Update variable names (#7104) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ci: Create ``tox.ini`` and ignore ``A003`` (#7123) * revert: Remove function name changes (#7104) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Rename tox.ini to .flake8 * Update data_structures/heap/heap.py Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com> * refactor: Rename `next_` to `next_item` (#7104) * ci(pre-commit): Add `flake8` plugin `flake8-bugbear` (#7127) * refactor: Follow `flake8-bugbear` plugin (#7127) * fix: Correct `knapsack` code (#7127) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
"""
|
|
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
|
|
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention,
|
|
1 is included.
|
|
Given an integer n, we have to find the nth ugly number.
|
|
|
|
For more details, refer this article
|
|
https://www.geeksforgeeks.org/ugly-numbers/
|
|
"""
|
|
|
|
|
|
def ugly_numbers(n: int) -> int:
|
|
"""
|
|
Returns the nth ugly number.
|
|
>>> ugly_numbers(100)
|
|
1536
|
|
>>> ugly_numbers(0)
|
|
1
|
|
>>> ugly_numbers(20)
|
|
36
|
|
>>> ugly_numbers(-5)
|
|
1
|
|
>>> ugly_numbers(-5.5)
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: 'float' object cannot be interpreted as an integer
|
|
"""
|
|
ugly_nums = [1]
|
|
|
|
i2, i3, i5 = 0, 0, 0
|
|
next_2 = ugly_nums[i2] * 2
|
|
next_3 = ugly_nums[i3] * 3
|
|
next_5 = ugly_nums[i5] * 5
|
|
|
|
for _ in range(1, n):
|
|
next_num = min(next_2, next_3, next_5)
|
|
ugly_nums.append(next_num)
|
|
if next_num == next_2:
|
|
i2 += 1
|
|
next_2 = ugly_nums[i2] * 2
|
|
if next_num == next_3:
|
|
i3 += 1
|
|
next_3 = ugly_nums[i3] * 3
|
|
if next_num == next_5:
|
|
i5 += 1
|
|
next_5 = ugly_nums[i5] * 5
|
|
return ugly_nums[-1]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
from doctest import testmod
|
|
|
|
testmod(verbose=True)
|
|
print(f"{ugly_numbers(200) = }")
|