Python Program 20 Pts Write Program Primalitytestpy Prompts User Integer N N 2 Determines Q43863290
Use Python Please
Python Program (20 pts) Write a program (primality_test.py ) that prompts the user for an integer, n , where n>=2 , determines if the input value is a prime number, and prints out the result. The program should handle invalid inputs and corner cases (eg. n<2) well and should not throw errors. Within the program, define a predicate function, is prime(n), that accepts an integer argument n and returns True if n is prime, False otherwise. To determine if a number is prime or not, implement the trial division approach described below. Notes on determining primality: • A natural number (.e. 1, 2, 3, 4, 5, 6, etc.) is called a prime number (or a prime) if it has exactly two positive divisors, 1 and the number itself. Natural numbers greater than 1 that are not prime are called composite. • Trial division approach details (adapted from Wikipedia) Given an input number n , check whether any prime integer m from 2 to yn evenly divides n (the division leaves no remainder). If n is divisible by any m then n is composite, otherwise it is prime. For example, we can do a trial division to test the primality of 100. Let’s look at all the divisors of 100: 2, 4, 5, 10, 20, 25, 50 If we take a closer look at the divisors, we will see that some of them are redundant. If we write the list differently: 100 = 2 x 50 = 4 x 25 = 5 x 20 = 10 x 10 = 20 x 5 = 25 x 4 = 50 x 2 100, the divisors just flip around and repeat. Therefore, we can further the redundancy becomes obvious. Once we reach 10, which is eliminate testing divisors greater than n. Example program outputs: Please enter an integer >= 2: 100 100 is not prime! Please enter an integer >= 2: 17 17 is prime! Please enter an integer >= 2: 97 97 is prime! Bonus (2 pts) Extend your previous program to prompt the user for another integer, M. Determines sum of all prime numbers from 2 to M (.e. sum of x , such that 2<=x<M ). Print the resulting sum to the console. For this part of the solution, implement a function called sum_primes (M) that accepts an integer M as an argument and returns the sum of all primes from 2 to M. Also, make sure the program handles invalid inputs and corner cases (eg. M<2) well and does not throw errors. Show transcribed image text Python Program (20 pts) Write a program (primality_test.py ) that prompts the user for an integer, n , where n>=2 , determines if the input value is a prime number, and prints out the result. The program should handle invalid inputs and corner cases (eg. n= 2: 100 100 is not prime! Please enter an integer >= 2: 17 17 is prime! Please enter an integer >= 2: 97 97 is prime! Bonus (2 pts) Extend your previous program to prompt the user for another integer, M. Determines sum of all prime numbers from 2 to M (.e. sum of x , such that 2
Expert Answer
Answer to Python Program (20 pts) Write a program (primality_test.py ) that prompts the user for an integer, n , where n>=2 , dete…
OR