MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / unique_prime_factors

Function unique_prime_factors

maths/prime_factors.py:50–87  ·  view source on GitHub ↗

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) [] >>> unique_prime_factors(10**

(n: int)

Source from the content-addressed store, hash-verified

48
49
50def unique_prime_factors(n: int) -> list[int]:
51 """
52 Returns unique prime factors of n as a list.
53
54 >>> unique_prime_factors(0)
55 []
56 >>> unique_prime_factors(100)
57 [2, 5]
58 >>> unique_prime_factors(2560)
59 [2, 5]
60 >>> unique_prime_factors(10**-2)
61 []
62 >>> unique_prime_factors(0.02)
63 []
64 >>> unique_prime_factors(10**241)
65 [2, 5]
66 >>> unique_prime_factors(10**-354)
67 []
68 >>> unique_prime_factors('hello')
69 Traceback (most recent call last):
70 ...
71 TypeError: '<=' not supported between instances of 'int' and 'str'
72 >>> unique_prime_factors([1,2,'hello'])
73 Traceback (most recent call last):
74 ...
75 TypeError: '<=' not supported between instances of 'int' and 'list'
76 """
77 i = 2
78 factors = []
79 while i * i <= n:
80 if not n % i:
81 while not n % i:
82 n //= i
83 factors.append(i)
84 i += 1
85 if n > 1:
86 factors.append(n)
87 return factors
88
89
90if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected