Created
June 5, 2017 18:31
-
-
Save mvallebr/cfb8edfc1a464bc6948aa97e320699f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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