Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Created June 5, 2017 18:31
Show Gist options
  • Select an option

  • Save mvallebr/cfb8edfc1a464bc6948aa97e320699f1 to your computer and use it in GitHub Desktop.

Select an option

Save mvallebr/cfb8edfc1a464bc6948aa97e320699f1 to your computer and use it in GitHub Desktop.
from collections import defaultdict, Counter, deque
import math
def primeFac(n):
prime_list = deque()
pivot = 2
current = n
while current != 1 and pivot <= math.sqrt(n):
if current % pivot == 0:
prime_list.append(pivot)
current //= pivot
else:
pivot += 1
if pivot > math.sqrt(n):
prime_list.append(current)
return prime_list
def generate_expression(l):
amounts = Counter(l)
return "".join(["({}**{})".format(i, amounts[i]) if amounts[i] !=1 else "({})".format(i) for i in sorted(amounts.keys())])
def primeFactors(n):
l = primeFac(n)
exp = generate_expression(l)
print (exp)
return exp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment