I'm hoping there is an easy solution to this. I can't seem to wrap my head around why this occurs. Everything online suggests this should not be the behavior I'm seeing from Python 3.8.
I'm working on Problem 3 on projecteuler.net. You have to find the prime factorization of a huge number, and multiply them altogether.
My idea was to first find every factor less than half of the original number (unrelated, but this works mathematically and saves time). Then I would simply iterate through the list of factors to remove any that weren't prime.
When I get to any factor that isn't prime, I use a break to get out of the nested loop which checks for and removes the nonprime ones, but it then proceeds to skip checking the next factor entirely as well. Essentially, it's behaving like a continue
statement on the outer loop.
I've tried many modifications, including a try and except, but nothing will stop the break
from skipping over the next factor.
Here is the code with extra print functions called for debugging purposes:
import math as m
def prime_factor(n):
factors = []
for i in range(1,m.ceil(n/2)):
if n % i == 0:
factors.append(i)
print(factors)
for num in factors:
print('******', num, '******')
for i in range(2, num):
if num % i == 0:
print('NOT PRIME', num, 'NOT PRIME')
factors.remove(num)
break
print(factors)
return factors
prime_factors = prime_factor(100)
Here is the output of that code if run:
[1, 2, 4, 5, 10, 20, 25]
****** 1 ******
****** 2 ******
****** 4 ******
NOT PRIME 4 NOT PRIME
****** 10 ******
NOT PRIME 10 NOT PRIME
****** 25 ******
NOT PRIME 25 NOT PRIME
[1, 2, 5, 20]
First list is of all factors. Then, it's supposed to check each factor, but since 10 was found to not be a factor, the inner loop is broken, and then 20 is skipped in the outer loop.
I really hope somebody finds this problem interesting enough to help me solve it, because I'm at a loss besides just trying an entirely different approach altogether.
Thanks for any and all help.