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
CMPS 12A Introduction to Programming Winter 2008 Programming Assignment 4 Due Tuesday February 19, 10:00 pm In this project you will write a Java program that reads a positive integer from standard input, then determines whether or not that number is prime. We say that an integer n is divisible by a non-zero integer d if there exists an integer k such that n k d , i.e. if d divides evenly into n. We also say in this context that d is a divisor of n. Recall that a positive integer n is called prime if its only positive divisors are 1 and n. The one exception to this rule is the number 1 itself, which is considered to be non-prime. A positive integer that is not prime is called composite. Euclid showed that there are infinitely many prime numbers. The prime and composite sequences begin as follows: Primes: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, … Composites: 1, 4, 6, 8, 9, 10, 12, 14, 15, 16, … There are many ways to test a number for primality, but perhaps the simplest is to just do trial divisions. Begin by dividing n by 2, and if it divides evenly, then n is not prime. Otherwise, divide by 3, then 4, then 5, etc. If at any point n is found to be divisible by a number d in the range 2 d n 1, then halt, and conclude that n is composite. Otherwise, conclude that n is prime. Actually, a moments thought reveals that is not necessary to go all the way up to n 1 . One need only do trial divisions of n by integers d in the range 2 d n . (To see this, suppose n 1 is composite. Then there exist positive integers a and b such that 1 a n , 1 b n , and n a b . But if both a n and b n , then a b n , contradicting that n a b . Hence one of a or b must be less than or equal to n .) You can implement this procedure in Java by writing a loop controlled by an int variable d, which starts at d 2 , and halts when either a trial division by d succeeds, or when d 2 n . It may be useful to declare a variable of type boolean called isPrime which is initialized to true, and whose value is recalculated on each iteration of the loop. isPrime can then be used as part of the loop repetition condition. To perform a trial division by d, one need only calculate the remainder of n upon division by d, and compare that remainder to zero. Recall that the remainder is given by the Java expression n%d. (For more on the integer division operation, see the pa3 project description.) Just as in pa3, your program will carry out a robust interaction with the user to check for correct input. In particular, if the user enters anything other a positive integer, your program will continue to prompt for more input. You must design appropriate loops to carry out these checks. It may be useful to study the program CheckInput2.java on the webpage to see how to do this. Format your program output to coincide with the sample runs below. % java Prime Enter a positive integer: 25.6 Please enter a positive integer: -15 Please enter a positive integer: xyz Please enter a positive integer: 73 73 is prime 1 % java Prime Enter a positive integer: 252525 252525 is composite % java Prime Enter a positive integer: 764321 764321 is prime % java Prime Enter a positive integer: 1 1 is composite Observe that non-positive integers, double values, and non-numeric strings are rejected until a positive integer is entered. It is recommended that you first write a program that does no input checking of any kind, and just determines the primality of a positive integer correctly. When this is done, implement the input checks described above. What to turn in Name your source file for this project Prime.java and submit it to the assignment name pa4 in the usual way: % submit cmps012a-pt.w08 pa4 Prime.java Use peek to check that submit was successful, then double check manually by following the instructions in pa1 and lab1. 2