Skip to content

Instantly share code, notes, and snippets.

@nick3499
Created May 17, 2022 04:42
Show Gist options
  • Save nick3499/702dfa15149b50a17938c8b50e0d8e5a to your computer and use it in GitHub Desktop.
Save nick3499/702dfa15149b50a17938c8b50e0d8e5a to your computer and use it in GitHub Desktop.
Prime Factorization
p = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}
n = 7775460
l = []
n1 = 0
while n not in p:
if n % 2 == 0:
n = n // 2
l.append(2)
else:
for num in [3, 5, 7, 11, 13, 17]:
if n % num == 0:
n = n // num
n1 = n
l.append(num)
break
else:
pass
if n in p:
l.append(n1)
else:
pass
s = ''
for n in sorted(list(set(l))):
if l.count(n) > 1:
s += f'({n}**{l.count(n)})'
else:
s += f'({n})'
print(s)
7775460 should return '(2**2)(3**3)(5)(7)(11**2)(17)'
/ \
2 3887730
/ \
2 1943865
/ \
3 647955
/ \
3 215985
/ \
3 71995
/ \
5 14399
/ \
7 2057
/ \
11 187
/ \
11 17
===============================================================
86240 should return '(2**5)(5)(7**2)(11)'
/ \
2 43120
/ \
2 21560
/ \
2 10780
/ \
2 5390
/ \
2 2695
/ \
5 539
/ \
7 77
/ \
7 11
=============================================
100 should return '(2**2)(5**5)'
/ \
2 50
/ \
2 25
/ \
5 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment