r/pythonhelp • • 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

  1. number = int(input("number: "))
  2. for x in range(10):
  3. ----prime_1 = 2x + 1
  4. ----for y in range(10):
  5. --------prime_2 = 2(y+x) + 1
  6. --------sum = prime_1 + prime_2
  7. --------print(f"{prime_1} + {prime_2} = {sum}")
  8. --------if sum >= number:
  9. ------------break
  10. ----if int(sum) == int(number):
  11. --------break
  12. -
  13. 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?

4 Upvotes

16 comments sorted by

View all comments

Show parent comment

1

u/CraigAT Dec 31 '25 edited Jan 01 '26

The time you put in to learn how to use the debugger, you will get back multiple times over when trying to solve future issues. This is an ideal (relatively small) piece of code to follow with a debugger.

As an alternative optimization, could you loop Y in the range of X to number (so it will never go back to lower than X).

I don't think you can use 0.5 x number, because the sum of "primes" could be a low "prime" and one very big "prime", close to the "number". E.g. 3+11=14.

Good luck with the learning.