Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Factors ERROR
#1
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python prediction on historical data and/or external factors. pyrooky 1 1,745 Sep-03-2020, 03:19 PM
Last Post: DPaul
  Python Finding Prime Factors foxman322 1 2,348 Jan-11-2019, 04:33 PM
Last Post: ichabod801
  Program that displays the number with the greatest amount of factors ilusmd 3 2,862 Nov-01-2018, 08:28 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020