Programming Exercise 9

  < Previous  Next >

Due Date

See the calendar for due date.

Assignment:

A prime number is a number that is only evenly divisible by itself and 1. Write a function named is_prime(N) where N is an integer argument and the function returns the first divisor if the argument is not prime, or 1 otherwise. For instance, if the input is 5, the function will return 1 whereas if the input was 6 it would return 2. Use the function in a program that prompts the user to enter a number and then prints whether the number is prime based on the return value. The program should also check that the input is positive and print an appropriate error message if not.

Examples

The following are examples of correct execution (with the text in bold being the input from the user):
Please enter a positive integer: 9
The integer 9 is not prime because it is divisible by 3
Please enter a positive integer: 11
The integer 11 is prime!
Please enter a positive integer: 100
The integer 100 is not prime because it is divisible by 2
Please enter a positive integer: 169
The integer 169 is not prime because it is divisible by 13
Please enter a positive integer: -3
-3 is not a positive integer.
Please enter a positive integer: -169
-169 is not a positive integer.
Please enter a positive integer: -9
-9 is not a positive integer.
Please enter a positive integer: 9
The integer 9 is not prime because it is divisible by 3

Submission

Submit your python script using the handin program. For handin, for this lab, type the following in a terminal window exactly as it appears:
handin  pe9  pe9.py
To verify your submission, type the following in a terminal window:
handin  pe9