Search code examples
pythonoutput

Goldbach Python Output


I made a python code for Goldbach conjecture. The thing is my output looks like this

Enter the lower limit: 8
Enter the Upper limit: 10
8 = 3 + 5
10 = 3 + 7
10 = 5 + 5

What I want my output to look like is

8 = 3 + 5
10 = 3 + 7 = 5 + 5

Is there any way to format it as such?

I am posting only the for loop:

for n in range (lo_limit, up_limit + 1): #lo_limit and up_limit is what you input
  if (n % 2 == 0):
    for a in range (1, n + 1): 
      if is_prime(a): #is_prime represent numbers that are prime
        for b in range(1, n + 1):
          if is_prime(b):
            if (a + b == n):
              if (a <= b):
                print(n, "=", a, "+", b)

main()


Solution

  • Try this:

    for n in range (lo_limit, up_limit + 1):
        if (int(n) % 2 == 0):
            printedstring=str(n)
            for a in range (1, n + 1): 
                if is_prime(a):
                    for b in range(1, n + 1):
                        if is_prime(b):
                            if (a + b == n):
                                if (a <= b):
                                    printedstring = printedstring + str(" = ", a, " + ", b)
            print(printedstring)