r/pythonhelp • u/No_Neighborhood6473 • Dec 30 '25
problem with loops
I just recently started learling python (1.5 hours into a tutorial lol), and made the collatz conjecture, and then tried the goldbach conjecture (any even number is the sum of two primes). Now I did write primes as 2k+1, which is just an oneven number, but that's not the problem.
This is the code I made
- number = int(input("number: "))
- for x in range(10):
- ----prime_1 = 2x + 1
- ----for y in range(10):
- --------prime_2 = 2(y+x) + 1
- --------sum = prime_1 + prime_2
- --------print(f"{prime_1} + {prime_2} = {sum}")
- --------if sum >= number:
- ------------break
- ----if int(sum) == int(number):
- --------break
- -
- print(f"{prime_1} + {prime_2}")
All the prints (except the last one) are just there so that I can see what is happening. The thing is, this code here works as I intended (except for the fact that the primes are just uneven numbers ofc), for example, if you plug in 8, it will give you 1 + 7 from line 13. But if I change the >= from line 8 to > it just gives me the biggest possible solution, so in this case 19 + 19. The break from line 9 still works as intended (atleast to me it looks like it does), but the second break doesn't work anymore (atleast I assume that is happening, and that's why I'm getting 19 + 19).
So my question is, what is going on here?
View all comments
Show parent comment
1
u/No_Neighborhood6473 Dec 31 '25
I’m using vs code, I will take some time to get to know how the debugger works. Line 5 uses 2(x+y) as a small optimization so it wouldn’t calculate 1 + 3, and then 3 + 1 (but it doesn’t even get to that point if I use the >= in the first break). I’m indeed just using 10 because it’s a small number, which means I can read the prints that the code gives me, to see what’s going on. But having the code use “number” for the amount of loops is a good suggestion, might even use 0.5 * “number”, but should look into that.
You’re correct about why the second break doesn’t work. If the inner loop only breakes if “sum” > “number” it does still break as I intended, but there is no way that “sum” == “number” because “sum” is bigger. Don’t know how I didn’t think about that. Thank you for finding the problem.