Question

In: Computer Science

use Python The assertion that every even number is the sum of two prime numbers is...

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...

Solutions

Expert Solution

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:


Related Solutions

Write a python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
complete in python The function sumEven should return the sum of only the even numbers contained...
complete in python The function sumEven should return the sum of only the even numbers contained in the list, lst. Example list_of_nums = [1, 5, 4, 8, 5, 3, 2] x = sum_evens(list_of_nums) print(x) #prints 14 Start your code with def evens(lst):
Java program Prime Numbers A prime number is a natural number which has exactly two distinct...
Java program Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a java program which reads a list of N integers and prints the number of prime numbers in the list. Input: The first line contains an integer N, the number of elements in the list. N numbers are given in the following lines. Output:...
1. Write a python function that receives two positive numbers and displays the prime numbers between...
1. Write a python function that receives two positive numbers and displays the prime numbers between them.Note: A prime number (or a prime) is a natural number greater than 1 and that has no positive divisors other than 1 and itself. 2. Using either Whileor Foriteration loops, write a python code that prints the first Nnumbers in Fibonacci Sequence, where N is inputted by the user. Now, revise this code to instead print a) the Nthnumber in Fibonacci Sequence.b) the...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum of odd numbers. c. the largest integer d. the smallest integer e. the total count of even numbers f. the total count of odd numbers. Using C++ program with for loop..
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
a)Find two positive numbers such that the sum of the first number and twice the second...
a)Find two positive numbers such that the sum of the first number and twice the second number is 108 and the product is a maximum b)figure out the dimentions of a rectangular solid that has a square base of maximum volume if its surface area is 216 square inches
Use the method of exhaustion to prove the following statement: “For every prime number p between...
Use the method of exhaustion to prove the following statement: “For every prime number p between 30 and 58, 10 does not divide p − 9.” Prove that 0.17461461 . . . is rational (digits 461 in the fractional part are periodically repeated forever).
show that every function can be expressed as the sum of an even function and an...
show that every function can be expressed as the sum of an even function and an odd function and that there is only one way to do this.
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number...
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number of input integers or float numbers. In other words, calls like this should all work: flexible_sum(x1, x2) # sum of two integer or float numbers or strings x1 and x2 flexible_sum(x1, x2, x3) # sum of 3 input parameters and can also handle a single input parameter, returning it unchanged and with the same type, i.e.:   flexible_sum(1) returns 1 and can accept an arbitrary...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT