I just started to open for myself programming in python. I know how to find factors for example of 30. I will get [1,2,3,5,6,10,15,30]. But I need this output 30 = 2 * 3 * 5. Thank you in advance!
Asked
Active
Viewed 3,853 times
0
-
Welcome to Stackoverflow! Please be sure to have a look at existing questions and answers before posting a new question. – hagello Mar 29 '17 at 05:47
1 Answers
0
Since you're trying to find all prime, unique factors, I'd use the following function:
def factor(numberToFactor, arr=list()):
i = 2
maximum = numberToFactor / 2 + 1
while i < maximum:
if numberToFactor % i == 0:
return factor(numberToFactor/i,arr + [i])
i += 1
return list(set(arr + [numberToFactor]))
print(factor(59511555)) # [3, 5, 1747, 757]

Neil
- 14,063
- 3
- 30
- 51