Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Assignment 5 Practice with Functions Write a program that computes prime numbers. A prime number, recall, is an integer >= 2 that is divisible only by itself and 1. This program will: Compute either the nth prime or primes in a range Not permit the user to proceed without having entered valid input Be modularized, that is, decomposed into several functions Execution: 1. The program will ask the user to input ‘r’ or ‘n’, where ‘r’ indicates a range of prime numbers, and ‘n indicates the nth prime number. The first five prime numbers are 2,3,5,7,11. 11 is the 5th prime number. 2. If the user enters anything except ‘r’ or ‘n’, the program displays “Invalid Entry” and returns to step 1. You can assume that your user will enter only alphabetic characters. 3. Steps 1 and 2, will require the following Boolean function: #returns True if inp is ‘r’ or ‘n’, returns False otherwise def check_inp(inp): 4. If the user enters ‘n’, the program computes and displays the nth prime number and exits. This step requires the following function: #displays the nth prime number def gen_nth(n): gen_nth requires the Boolean function is_prime, documented in step 7 5. If the user enters ‘r’, the program requests two integers in the range [low … high]. low is >= 2 and low < high. You can assume that your user will enter integers. This step requires the following Boolean function: #returns True if parameters meet the range criteria, False otherwise def check_range(low, high) If check_range(low, high) returns False, the program displays an error message and requests the integers again. 6. If the range meets the stated criteria, the program will invoke this function and exit: #displays all prime numbers in the range [low .. high] def gen_range(low, high) gen_range requires the Boolean function is_prime, documented in step 7. 7. Both gen_nth and gen_range that generate require the following Boolean function: #returns True if n is prime, False otherwise def is_prime(n): To do this problem, proceed incrementally: A. B. C. D. E. Code and test steps 1, 2, 3 Code and test step 5 Code and test step 7 Code and test step 4 Code and test step 6 Bonus: Once all steps have been thoroughly tested, see if you can refine is_prime(n). There is an obvious way to check for primality and a less obvious, but much more efficient way. Hint: an integer that is not prime is composite. A composite integer has (at least) two factors. Decompose a few small integers into two factors. Do you see a pattern in the sizes of the factors with respect to the composite integer?