Python Forum
Factors ERROR - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Factors ERROR (/thread-21964.html)



Factors ERROR - AdamJae - Oct-22-2019

def factors(n):
    list = []
    for x in range(2, n+1):
       while n % x == 0:
           list.append(x)
           n = n//x
       if n == 1:
           return list
I produced some code to give me a list of prime factors of any number. But when I execute the code it produces this:
factors(10075)
Out[99]: [5, 5, 13, 31]
It pastes two of the same prime factor, why is this?
Need URGENT help, please!
Thank you all <3


RE: Factors ERROR - buran - Oct-22-2019

because that is what your code is doing
5*5*13*31 = 10075
if you don't want repeating factors - you can use set() to get just unique elements from your list
and don't use list as variable name - it's a built-in function. you don't want to override it


RE: Factors ERROR - AdamJae - Oct-22-2019

Oh, I am so stupid... Thank you very much!