0
Fix my python code...
def factors(n) : L=[] if is_prime(n) : L.append(n) else : b=math.floor(math.sqrt(n))+1 D=[x for x in range(2,b) ] for d in D: if n%d==0: L.append(d) break L.extend(factors(int(n/d))) return L Ive written python code to find prime factors of integer of n. My question is how to fix this code so that it can't replace L to L=[ ] when function is called second time ( factors(n/d) )?
2 Respostas
+ 11
Hi, just a wee reminder, could you please use the Code Playground to share your code next time? It makes it easier for us to help you, thanks 😊
+ 6
Could you please share the whole code?
From what I see you would like to iterate through the range of 2 till sqrt(n). Perhaps you could replace your if statement with a while loop and check for every consecutive number from 2 to sqrt(n)?
Or better yet, start off with an empty list L and .append() to it only those numbers by which modulo of x equals 0 and is_prime() is True.