Skip to content

Instantly share code, notes, and snippets.

@PurpleMyst
Created December 10, 2017 22:47
Show Gist options
  • Save PurpleMyst/e2e2e3311f1c5a1b48599e03f433ab80 to your computer and use it in GitHub Desktop.
Save PurpleMyst/e2e2e3311f1c5a1b48599e03f433ab80 to your computer and use it in GitHub Desktop.
#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment