Hello Everybody,
I am suppose to "write a Java program that reads a positive integer n from standard input, then prints out the first n prime number." It's divided into 3 parts.
1st: This function will return true or false according to whether m is prime or composite. The array
argument P will contain a sufficient number of primes to do the testing. Specifically, at the time
isPrime() is called, array P must contain (at least) all primes p in the range 2 p m . For instance,
to test m = 53 for primality, one must do successive trial divisions by 2, 3, 5, and 7. We go no further
since 11 53 . Thus a precondition for the function call isPrime(53, P) is that P[0] = 2 , P[1] = 3 ,
P[2] = 5, and P[3] = 7 . The return value in this case would be true since all these divisions fail.
Similarly to test m =143 , one must do trial divisions by 2, 3, 5, 7, and 11 (since 13 143 ). The
precondition for the function call isPrime(143, P) is therefore P[0] = 2 , P[1] = 3 , P[2] = 5, P[3] = 7 ,
and P[4] =11. The return value in this case would be false since 11 divides 143. Function isPrime()
should contain a loop that steps through array P, doing trial divisions. This loop should terminate when
2
either a trial division succeeds, in which case false is returned, or until the next prime in P is greater
than m , in which case true is returned.
Then there is the "main function"
• Check that the user supplied exactly one command line argument which can be interpreted as a
positive integer n. If the command line argument is not a single positive integer, your program
will print a usage message as specified in the examples below, then exit.
• Allocate array Primes[] of length n and initialize Primes[0] = 2 .
• Enter a loop which will discover subsequent primes and store them as Primes[1] , Primes[2],
Primes[3] , ……, Primes[n -1] . This loop should contain an inner loop which walks through
successive integers and tests them for primality by calling function isPrime() with appropriate
arguments.
• Print the contents of array Primes[] to stdout, 10 to a line separated by single spaces. In other
words Primes[0] through Primes[9] will go on line 1, Primes[10] though Primes[19] will go
on line 2, and so on. Note that if n is not a multiple of 10, then the last line of output will contain
fewer than 10 primes.
The last function is called "usage" which I am not sure how to execute this!
Your program will include a function called Usage() having signature
static void Usage()
that prints this message to stderr, then exits. Thus your program will contain three functions in all:
main(), isPrime(), and Usage(). Each should be preceded by a comment block giving it’s name, a
short description of it’s operation, and any necessary preconditions (such as those for isPrime().)
And hear is my code, but I am having a bit of a problem and could you guys help me fix it? If I enter the number "5" it gives me the prime numbers which are "6,7,8,9" which doesn't make much sense.
import java.util.;
import java.io.;
import java.lang.*;
public class PrimeNumber {
static boolean isPrime(int m, int[] P){
int squarert = Math.round( (float)Math.sqrt(m) );
int i = 2;
boolean ans=false;
while ((i<=squarert) & (ans==false))
{
int c= P[i];
if (m%c==0)
ans= true;
else
ans= false;
i++;
}
/* if(ans ==true)
ans=false;
else
ans=true;
return ans;
}
///****main
public static void main(String[] args ) {
Scanner in= new Scanner(System.in);
int input= in.nextInt();
int i, j;
int squarert;
boolean ans = false;
int userNum;
int remander = 0;
System.out.println("input: " + input);
int[] prime = new int[input];
prime[0]= 2;
for(i=1; i
ans = isPrime(j,prime);
j++;}
prime[i] = j;
}
//prnt prime
System.out.println("The first " + input + " prime number(s) are: ");
for(int r=0; r
}//end of main
}
Thanks for the help