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)
| 22 | |
| 23 | |
| 24 | def 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 |