mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
bc8df6de31
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2) - [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
54 lines
1.1 KiB
Python
54 lines
1.1 KiB
Python
"""
|
|
python/black : True
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
def prime_factors(n: int) -> list[int]:
|
|
"""
|
|
Returns prime factors of n as a list.
|
|
|
|
>>> prime_factors(0)
|
|
[]
|
|
>>> prime_factors(100)
|
|
[2, 2, 5, 5]
|
|
>>> prime_factors(2560)
|
|
[2, 2, 2, 2, 2, 2, 2, 2, 2, 5]
|
|
>>> prime_factors(10**-2)
|
|
[]
|
|
>>> prime_factors(0.02)
|
|
[]
|
|
>>> x = prime_factors(10**241) # doctest: +NORMALIZE_WHITESPACE
|
|
>>> x == [2]*241 + [5]*241
|
|
True
|
|
>>> prime_factors(10**-354)
|
|
[]
|
|
>>> prime_factors('hello')
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: '<=' not supported between instances of 'int' and 'str'
|
|
>>> prime_factors([1,2,'hello'])
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: '<=' not supported between instances of 'int' and 'list'
|
|
|
|
"""
|
|
i = 2
|
|
factors = []
|
|
while i * i <= n:
|
|
if n % i:
|
|
i += 1
|
|
else:
|
|
n //= i
|
|
factors.append(i)
|
|
if n > 1:
|
|
factors.append(n)
|
|
return factors
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|