Reputation: 13
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()
Upvotes: 1
Views: 794
Reputation: 189
Here is my take. You just need to keep concatenating f" = {x} + {n-x}" to a string then print the string:
def is_prime(n):
if (n <= 3): return n > 1
if n%6 not in [1,5]: return False # 6k numbers
limit = int(n**.5)+1
for i in range(5,limit+1,6):
if (n%i == 0 or n%(i+2) == 0): return False
return True
def goldbach2(lo_limit,up_limit):
lo_limit = lo_limit+lo_limit%2 # next even
for n in range (lo_limit,up_limit + 1,2): #lo_limit and up_limit is what you input
ans = ""
if n == 4: ans += " = 2 + 2"
if is_prime(n-3): ans += f" = 3 + {n-3}"
for p,q in ((i,i+2) for i in range(5,n//2+1,6)):
if is_prime(p) and is_prime(n-p): ans += f" = {p} + {n-p}"
if is_prime(q) and is_prime(n-q): ans += f" = {q} + {n-q}"
print("\n",n, "=", ans)
goldbach2(110,124)
Output:
$ python3 gold_range.py
110 = = 3 + 107 = 7 + 103 = 13 + 97 = 31 + 79 = 37 + 73 = 43 + 67
112 = = 3 + 109 = 5 + 107 = 11 + 101 = 23 + 89 = 29 + 83 = 41 + 71 = 53 + 59
114 = = 5 + 109 = 7 + 107 = 11 + 103 = 13 + 101 = 17 + 97 = 31 + 83 = 41 + 73 = 43 + 71 = 47 + 67 = 53 + 61
116 = = 3 + 113 = 7 + 109 = 13 + 103 = 19 + 97 = 37 + 79 = 43 + 73
118 = = 5 + 113 = 11 + 107 = 17 + 101 = 29 + 89 = 47 + 71 = 59 + 59
120 = = 7 + 113 = 11 + 109 = 13 + 107 = 17 + 103 = 19 + 101 = 23 + 97 = 31 + 89 = 37 + 83 = 41 + 79 = 47 + 73 = 53 + 67 = 59 + 61 = 61 + 59
122 = = 13 + 109 = 19 + 103 = 43 + 79 = 61 + 61
124 = = 11 + 113 = 17 + 107 = 23 + 101 = 41 + 83 = 53 + 71
Upvotes: 0
Reputation: 47820
Your function can be simplified and sped up a ton with some simple changes:
def goldbach(lo, hi):
# 1. just step by 2 instead of checking for even numbers
for n in range(lo, hi + 1, 2):
# 2. keep a list of found matches instead of building up a string
matches = [str(n)]
# 3. for any 'a', you can just subtract to find 'b' instead of looping
# 4. instead of testing for a <= b, just run the 'a' loop halfway
for a in range(1, n // 2 + 1):
if is_prime(a) and is_prime(n-a):
matches.append('{} + {}'.format(a, n-a))
# 5. join up the matches and print at the end
print(' = '.join(matches))
The entire inner for loop could be expressed as a list-comprehension as well, for more brevity.
You could easily optimize this further by generating a list of primes in your range beforehand, and then just iterating over those and checking membership for the complements, instead of doing repeated primality testing.
Upvotes: 1
Reputation: 626
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)
Upvotes: 0