Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions maths/prime_factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,50 @@
return factors


def unique_prime_factors(n: int) -> list[int]:
"""
Returns unique prime factors of n as a list.

>>> unique_prime_factors(0)
[]
>>> unique_prime_factors(100)
[2, 5]
>>> unique_prime_factors(2560)
[2, 5]
>>> unique_prime_factors(10**-2)
[]
>>> unique_prime_factors(0.02)
[]
>>> x = unique_prime_factors(10**241) # doctest: +NORMALIZE_WHITESPACE
>>> x == [2, 5]
True
>>> unique_prime_factors(10**-354)
[]
>>> unique_prime_factors('hello')
Traceback (most recent call last):
...
TypeError: '<=' not supported between instances of 'int' and 'str'
>>> unique_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
if i not in factors:
factors.append(i)
if n > 1:
if n not in factors:

Check failure on line 89 in maths/prime_factors.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM102)

maths/prime_factors.py:88:5: SIM102 Use a single `if` statement instead of nested `if` statements
factors.append(n)
return factors


if __name__ == "__main__":
import doctest

Expand Down
Loading