Lab Project5

advertisement
Comp 110L
11 Oct 2010
Putnam
35 points
( Individual Project)
Lab Project 5
Instructions: This must be your own work, do not request help from, nor provide help
to, your neighbors.
Your program must have
Read this document!
Project # 5: Prime Numbers
Section # <number>, i.e., Section 14201
Programmer: <name>
Date: <date submitted>
Description: <short statement of what the program will accomplish>
embedded at the top of your program.
The file should be saved as Project5<your_last_name>, hence the main Class should
also be labeled as Project5<your_last_name>, e.g., if I were writing the program, I
would have to label my main Class & file as Project5Putnam.
Your program must be built up using the following instructions.
1. Write a main program that calls the isPrime method given below and prints all
prime numbers n such that 1 <= n <= 100.
public static boolean isPrime(int k)
{
if( k < 1)
return false;
else if(( k == 2)||( k == 1))
return true;
else
{
for( int d = 2; d <= k/2; d++)
if( k % d ==0)
return false;
return true;
}
m=0
while (n != 0)
n = 7854
}
r = n%10
2. Create the following method
n = n/10
m = m*10 + r
public static int reversePrimeInt(int)
to produce the reverse of an integer, e.g., reversePrimeInt(4587) returns 7854
3. Add the reversePrimeInt method to the program; use it to print the reverse of all
prime numbers n such that 1 <= n <= 100. The output should list the numbers in
pairs (prime number, reverse of prime number), e.g., (1 ,1)(2, 2) … (11, 11) (13, 31)
(17, 71) (19, 91) … .
4. Copy and paste the results at the bottom of your program. Label it as DataTest #1.
5. An emirp is a prime number whose reversal is also a prime number, e.g., since 17
and 71 are both prime numbers then both 17 and 71 are emirps.
6. Using the isPrime method and the reversePrimeInt method, write an isEmirp method
public static boolean isEmirp(int)
having the header
that determines whether an integer is an emirp.
7. Modify the main program by
a. eliminating the direct calls to both the isPrime and reverseInt methods by
making those statements into comments; do not comment out the isPrime or
reverseInt method definitions.
b. calling the isEmirps method to display the first 100 emirps, printed ten
numbers per line and aligned as follows:
2
3
5
7
11
13
17
31
37
71
73
79
97
101
107
113
131
149
151
157
Run the program to produce the requested output.
Copy and paste the results at the bottom of your program. Label it as DataTest #2.
8. Twin primes are primes that differ by 2, e.g., 3 & 5 are twin primes.
9. Write an isTwinPrime method with the header
public static boolean isTwinPrime(int)
that determines if an integer is the first member of a pair of prime numbers.
10.
Add the isTwinPrime method to the main program and display all twin primes less
than 1000, printed as pairs:
(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), …
Copy and paste the results at the bottom of your program. Label it as DataTest
#3.
11.
Print the program with all the output pasted as comments at the bottom of the
program.
12. Your name must be in the program file name to get credit for the program.
Download