Skip to content

Commit 53745c2

Browse files
committed
Added Decomposing_factors.py
1 parent 4d45595 commit 53745c2

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Decomposing_factors.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def prime_factors(n):
2+
final = []
3+
rstr = ''
4+
while n != 1:
5+
for x in range(2,n+1):
6+
if n % x == 0:
7+
n //= x
8+
final.append(x)
9+
break
10+
for x in final:
11+
if f'({x}**{final.count(x)})' in rstr:
12+
continue
13+
rstr += f'({x}**{final.count(x)})' if final.count(x) > 1 else f'({x})'
14+
return rstr
15+
16+
#Function that decomposes number into their base multiplicants. Usefull for simplifications of square roots etc.
17+
#Example: prime_factors(30) outputs (2)(3)(5) because 30 = 2 * 3 * 5

0 commit comments

Comments
 (0)