1

Created a program to find prime factors of a natural number but i get an error:

multipliers = []
a = 2
value = input("Put a natural number here: ")
value = int(value)

for i in range(1, value):
    if value % i == 0:
        multipliers.append(i)

for x in multipliers:
    while a < x:
        if x % a == 0:
            multipliers.remove(x)
        else:
            a += 1

print(multipliers)

All I want to do here is: Get an input value, find the multipliers of a value and make a list from them, take theese multipliers 1 by 1 and try to divide by [2, 3, 4, 5...], if a is a multiplier of x delete it from the list and get another value from list as x and do the same.

But when I try to run this i get an error message that says

ValueError: list.remove(x): x not in list

I don't know where am I doing wrong. Can you please help me?

  • 2
    Thats because you're mutating the list while removing items from it. – scharette Aug 03 '18 at 15:04
  • Try making a new list consisting values you want, instead of removing items from your first list. That means reversing the condition in your second if statement and using append instead of remove – Gudarzi Aug 03 '18 at 15:08
  • @scharette try to run this with numbers like 20 –  Aug 03 '18 at 15:11
  • Well i tried to work with 2 lists and @Alfie 's solution on comments. They both worked. Thanks :) –  Aug 03 '18 at 15:15

1 Answers1

0

Because x is removed from the list - then the while loop loops again and it tries to remove x from the list again.

To fix this add a break in your while loop like so:

for x in multipliers:
    while a < x:
        if x % a == 0:
            multipliers.remove(x)
            break
        else:
            a += 1

You could also replace the second for loop with a list comprehension:

factors = [x for x in multipliers if all(x % a != 0 for a in range(2, x))]
Ed95
  • 58
  • 1
  • 6
Alfie
  • 1,903
  • 4
  • 21
  • 32
  • It works well but i can't use this program with the numbers like "600851475143". I don't know why. Can you please help me? –  Aug 03 '18 at 15:21
  • @EmreUYGUN memory error? – Alfie Aug 03 '18 at 15:22
  • No, there is nothing in command line when i enter this number. –  Aug 03 '18 at 15:23
  • @EmreUYGUN does the program terminate? – Alfie Aug 03 '18 at 15:25
  • No. there is just a blank. seems like not working with theese numbers. But I don't get it why –  Aug 03 '18 at 15:26
  • @EmreUYGUN using numbers like that your computer would have to loop an insane amount of times, its probably the case that it just takes a few minutes to run - experiment with it with slightly smaller numbers and see if you cans see the speed dropping off is my advice! – Alfie Aug 03 '18 at 15:28
  • Get it :). There is a speed drop as you say. But i waited 5 min and nothing happened with this high num. Is there a way to get rid of this? edit: waiting more is the way to get rid of this. Thanks @Alfie –  Aug 03 '18 at 15:33
  • @EmreUYGUN well your program is going to slow down no matter what as you use larger numbers - the only thing you can do is look into making it more efficient! :) – Alfie Aug 03 '18 at 15:36
  • @EmreUYGUN you should do some research into efficient prime number generates - there will be some interesting stuff there :) – Alfie Aug 03 '18 at 15:37