Guest User

Untitled

a guest
Dec 13th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import functools
  3. import itertools
  4. import math
  5.  
  6.  
  7. def prime_factors(n, sqrt=math.sqrt):
  8. limit = sqrt(n) + 1
  9.  
  10. i = 2
  11. while i <= limit:
  12. entered = False
  13.  
  14. while n % i == 0:
  15. entered = True
  16. n //= i
  17. yield i
  18.  
  19. if entered:
  20. limit = 1 + sqrt(n)
  21. i += 1
  22.  
  23. if n > 1:
  24. yield n
  25.  
  26.  
  27. def all_factors(n,
  28. combinations=itertools.combinations,
  29. reduce=functools.reduce,
  30. mul=lambda x, y: x * y):
  31. yield 1
  32.  
  33. ps = list(prime_factors(n))
  34.  
  35. for i in range(1, len(ps) + 1):
  36. for ms in combinations(ps, i):
  37. yield reduce(mul, ms, 1)
Add Comment
Please, Sign In to add comment