Using Python 3.6.1 One must use this function in the program: In 1742, a German
ID: 3803337 • Letter: U
Question
Using Python 3.6.1
One must use this function in the program:
In 1742, a German amateur mathematician named Christian Goldbach wrote to Leonhard Euler with the following conjecture Every n ber greater than 2 can be written as the s of three pr umbers. In his conjecture, Goldbach was considering 1 as a prime number. By convention we no longer consider 1 as a prime number. And Euler later modified the conjecture as follows: Every even number greater than or equal to 4 cam be expressed as the su of nwo prime mummbers. For example 20-3 7 7 13 42 35 37 31 3 29 9 23 There is no formal proof of this conjecture. However, we can verify Goldbach's conjecture in certain ranges. For this programming assignment you are asked to verity Goldbach's conjecture in a user defined range. You will prompt the user to enter the range and then your program will print out all the even numbers. in that range (inclusive of the end points in the form m a b, where a and b are prime numbers and a b. Each even number should be on a separate line followed by all possible unique pairs of prime numbers. Your sample output should look like this Enter the lower limit: 4 Enter the upper limit: 100 2 2 3 3 8 3 5 10 5 5 100 You will do the following error checking on the input: Lower limit is equal to or greater than 4 Both lower limit and upper limit are even Lower limit is strictly less than upper limit Even if only one of these conditions fails, prompt the user to enter both the limits repeatedly.Explanation / Answer
import sys
def is_prime (n):
if (n == 1):
return False
limit = int (n ** 0.5) + 1
divisor = 2
while (divisor < limit):
if (n % divisor == 0):
return False
divisor += 1
return True
def find(n):
for i in range(2,n):
if is_prime(i) and is_prime(n-i):
return [i,n-i]
low = input("Enter the lower limit:")
upp = input("Enter the upper limit:")
try:
low = int(low)
upp = int(upp)
except:
print("Input Error")
sys.exit()
if(low<4 or low%2 != 0 or upp<low or upp%2 != 0 ):
print("Input Error")
sys.exit()
for i in range(low,upp+1,2):
#print(i)
l = find(i)
s = str(i)+" = "+str(l[0])+" + "+str(l[1])
print(s)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.