4 ms·
> Can you spot all the problems? You were probably being rhetorical, but there are two problems: - `p = 2` should be outside the loop - `prime_factors.append
by justinpombrio 2y ago
> Can you spot all the problems?
You were probably being rhetorical, but there are two problems:
- `p = 2` should be outside the loop
- `prime_factors.append(n)` appends `1` onto the end of the list for no reason
With those two changes I'm pretty sure it's correct.
- deleted 2y ago[deleted]
- dangsux 2y ago[dead]
- rmbyrro 2y ago`n` isn't defined
- justinpombrio 2y agoThe implicit context that the poster removed (as you can tell from the indentation) was a function definition: def factorize(n): ... return prime_factors
- kenjackson 2y agoYou don't need to append 'p' in the inner while loop more than once. Maybe instead of an array for keeping the list of prime factors do it in a set.
- zeroonetwothree 2y agoIt’s valid to return the multiplicity of each prime, depending on the goal of this.
- justinpombrio 2y agohttps://www.wolframalpha.com/input?i=prime+factors+of+12 https://www.wolframalpha.com/input?i=prime+factors+of+12