Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import functools
- import itertools
- import math
- def prime_factors(n, sqrt=math.sqrt):
- limit = sqrt(n) + 1
- i = 2
- while i <= limit:
- entered = False
- while n % i == 0:
- entered = True
- n //= i
- yield i
- if entered:
- limit = 1 + sqrt(n)
- i += 1
- if n > 1:
- yield n
- def all_factors(n,
- combinations=itertools.combinations,
- reduce=functools.reduce,
- mul=lambda x, y: x * y):
- yield 1
- ps = list(prime_factors(n))
- for i in range(1, len(ps) + 1):
- for ms in combinations(ps, i):
- yield reduce(mul, ms, 1)
Add Comment
Please, Sign In to add comment