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

Function unique_prime_factors

project_euler/problem_047/sol1.py:24–46  ·  view source on GitHub ↗

Find unique prime factors of an integer. Tests include sorting because only the set matters, not the order in which it is produced. >>> sorted(set(unique_prime_factors(14))) [2, 7] >>> sorted(set(unique_prime_factors(644))) [2, 7, 23] >>> sorted(set(unique_prime_fact

(n: int)

Source from the content-addressed store, hash-verified

22
23
24def unique_prime_factors(n: int) -> set:
25 """
26 Find unique prime factors of an integer.
27 Tests include sorting because only the set matters,
28 not the order in which it is produced.
29 >>> sorted(set(unique_prime_factors(14)))
30 [2, 7]
31 >>> sorted(set(unique_prime_factors(644)))
32 [2, 7, 23]
33 >>> sorted(set(unique_prime_factors(646)))
34 [2, 17, 19]
35 """
36 i = 2
37 factors = set()
38 while i * i <= n:
39 if n % i:
40 i += 1
41 else:
42 n //= i
43 factors.add(i)
44 if n > 1:
45 factors.add(n)
46 return factors
47
48
49@lru_cache

Callers 1

upf_lenFunction · 0.70

Calls 1

addMethod · 0.45

Tested by

no test coverage detected