In: Computer Science
use Python
The assertion that every even number is the sum of two prime numbers is called Goldbach’s conjecture.
You will write a program that asks the user for an integer number, then checks if the integer is even, and finally determines two prime numbers that sum to the user’s entered integer number.
Assignment Requirements
Three functions are required:
get_input():
This function takes no parameters, but will ask the user for an integer number. It will return a valid integer. You will use an indefinite loop and exception handling like we discussed in class. It is required to catch user input error and gracefully recover. Learn about the continue keyword and how it works in a loop, it can be handy.
is_prime(n):
This function will take an integer parameter n, and return True if the number is prime, or return False if it is not prime. You can use this pseudocode for primality testing to write this function.
main():
This does the bulk of your program’s work for solving Goldbach’s conjecture. You will call get_input() and is_prime(n) from main().
Goldbach’s conjecture itself will be solved using an indefinite loop. Particularly a ‘post-test’ loop pattern. A for loop will not be accepted as part of the solution.
Sample output
A successful run:
This program tests the Goldbach's conjecture Please enter an even integer larger than 2: 36 36 = 5 + 31
A run recovering from errors:
This program tests the Goldbach's conjecture Please enter an even integer larger than 2: 7 Wrong input! Please enter an even integer larger than 2: foo Bad input! Please enter an even integer larger than 2: 8 8 = 3 + 5
If by chance the number chosen is valid, but doesn’t hold for Goldbach’s conjecture, then print:
Goldbach's conjecture doesn't hold for [whatever the number is]
(I’m not aware of a number for which this occurs, but handle this possible case anyways.)
HUGE THANK YOU TO ANYONE WHO'S ANSWERING...
Hi,
Hope you are doing fine. I have coded the above question in python keeping all the conditions in mind. The code has simple logic and has been clearly explained using comments that have been highlighted in bold. Also find the code snippet that will help you get an idea about the correct indentation of the code from editor.
Program:
#function to get input from user
def get_input():
#looping indefinitely
while True:
#taking input from user
num=input('Please enter an even integer larger than 2: ')
#try block
try:
#converting num to int. If it is a sstring then except
block catches it
num=int(num)
#checking if num is even
if num%2==0 and num>0:
#loop breaks if the enteres input is even
break
#if the number is odd or negative
else:
print("Wrong input!")
#we continue to the next iteration asking for input
again
continue
#it is executed when give a string as input instead of
number
except:
print("Bad input!")
#we continue to the next iteration asking for input
again
continue
#returning the num
return num
#function to check if a number is prime
def is_prime(n):
#i is use to check divisors of n
i=2
#count is used to count of n
count=0
#any number will not have divisors that are greater than
half of it!
while i<=n/2:
#if i divides n then increment count
if n%i==0:
count+=1
i+=1
#if n>1 and count is still 0 return True
if count==0 and n>1:
return True
#else return false
else:
return False
#Main
print("This program tests the Goldbach's conjecture")
#calling get_input() to get input and storing it in
num
num=get_input()
#i is used to check the divisors whose sum will give
num
i=2
#x and y will store results
x=-1
y=-1
#looping indefinitely
while True:
#if i is prime
if is_prime(i):
#check if num-i is also prime
if is_prime(num-i):
#store the values in x,y and break out from
loop
x=i
y=num-i
break
#incrementing i
i+=1
#if i is greater than or equal to num, then we break out of
the loop
if i>=num:
break
#if values of x and y remain as -1, it means that
goldbach's conjecture does not hold
if x==-1 and y==-1:
print("Goldbach's conjecture doesn't hold for ",num)
#else print result
else:
print("{} = {} + {}".format(num,x,y))
Executable code snippet:
Sample outputs: