Monday, July 27, 2009

Java help.............................

can any one explain what this means feel free to give an easier program i would really like an easier program





boolean flag=true;


int i=4;


int c=0;


for(c=2;c%26lt;i;c++){


if((i%c)==0)


{


flag=false;


break;


}


else


flag=true;


}


if(flag)


System.out.println("Prime no");


else


System.out.println("Not a Prime no.");





taken from





class Prime


{


public static void main(String[] args){


boolean flag=true;


int i=4;


int c=0;


for(c=2;c%26lt;i;c++){


if((i%c)==0)


{


flag=false;


break;


}


else


flag=true;


}


if(flag)


System.out.println("Prime no");


else


System.out.println("Not a Prime no.");


}


}

Java help.............................
This program intends to test a single number i as a prime number or not. This program isn't very flexible since it hard-codes the value i=4 instead of accepting a number from the command line prompt. Also, it's inefficient since the loop checks the numbers from 2 to i-1 instead of the square root of i. Note: the (i%c)==0 check is using the modulo operator to quickly determine if i is divisible by c, if so then i isn't a prime number. Better/shorter code (using the same lousy variable names) might be:





class Prime {


public static void main(String[] args) {


boolean flag=true;


int i=4;


for (int c = 2; c*c %26lt;= i; c++) {


if (i % c == 0) {


flag = false;


break;


} }


if (flag) System.out.println("Prime number");


else System.out.println("Not a Prime number");


} }





Adding a few comments for clarity:





class Prime { // prime number tester


public static void main(String[] args) {


int i=4; // check this number for primality


boolean flag=true; // assume the number will be prime


// if i has a factor that it has one less than or equal to sqrt(i),


// so for efficiency we only need to check c %26lt;= sqrt(i) or equivalently c*c %26lt;= i


for (int c = 2; c*c %26lt;= i; c++) {


if (i % c == 0) { // if c divides evenly into i, i is not prime, so break out of loop


flag = false; // i is not prime


break; // we're done checking early, so break out of this loop


} }


if (flag) System.out.println("Prime number");


else System.out.println("Not a Prime number");


} }





We'll generalize the code to accept a number from the command line prompt plus get rid of the last else statement:





class Prime { // prime number tester


public static void main(String[] args){


int i = Integer.parseInt(args[0]); // check the command line number for primality


boolean flag=true; // assume the number will be prime


// if i has a factor that it has one less than or equal to sqrt(i),


// so for efficiency we only need to check c %26lt;= sqrt(i) or equivalently c*c %26lt;= i


for (int c = 2; c*c %26lt;= i; c++) {


if (i % c == 0) { // if c divides evenly into i, i is not prime, so break out of loop


flag = false; // i is not prime


break; // we're done checking early, so break out of this loop


} }


if (!flag) System.out.print("Not a "); // i is not prime


System.out.println("Prime number");


} }





Finally, we'll make this program even shorter but possibly lose some clarity in the process. The flag is eliminated entirely:





class Prime { // prime number tester


public static void main(String[] args){


int i = Integer.parseInt(args[0]); // check the command line number for primality


// if i has a factor that it has one less than or equal to sqrt(i),


// so for efficiency we only need to check c %26lt;= sqrt(i) or equivalently c*c %26lt;= i


for (int c = 2; c*c %26lt;= i; c++) {


if (i % c == 0) { // if c divides evenly into i, i is not prime, so break out of loop


System.out.print("Not a "); // i is not prime


break; // we're done checking early, so break out of this loop


} }


System.out.println("Prime number");


} }





I hope you found this walk-through of changes instructive. Often, an iterative pairing down or even refactoring can work well with each change still leaving a working program but introducting gradual improvements. Of course, it would have been much better if the original would have had commented code in the first place...
Reply:I'm not entirely sure what you're asking. If you want to know what the program does, it looks like it should print whether the number 4 (or whatever the value of the variable i) is a prime number or not.





It uses the modulo (%) function to determine if there's a remainder when dividing i by the various numbers below it, and a simple 'for' loop to increment the other variable.





The rest of the program is just dealing with the message output under the various conditions.





That's about as simple as you're going to get for that program.
Reply:Sorry, this seems to be pretty simple enough.


The only thing i can advice is change the variable names, and the lines





else


flag=true;





are not necessary.





Let me try to explain it to you.





The goal is to find out whether a certain number is prime or not. By definition, a prime number is that which can be divided by some number other than itself and 1 (without a remainder or, well, zero remainder).





Here, the number we are testing is "i", and for the current instance, we are testing for whether i, which has a value of 4 is prime or not. But, for our example, can we please try i=9 instead? It would be much clearer. Ok?





Here's the plan. We first assume that 9 is prime. we test numbers 2 to 8, and whether it will divide i which for our case is 9 perfectly. Why 2 to 8? Well, we can't use 1, since by definition, we should look for a number other than 1 and itself, so we start with 2. We only try up to 8 since, well, any number greater than 8 would obviously lead to a remainder. (Ex: 9 div 11 = 0 remainder 9) If any of these numbers divide 9 perfectly, we'd know that 9 is NOT a prime. Here's also a trick, if we already find a number which divides 9 without a remainder, why bother testing the other numbers? just quit!





anyway... here's how it applies to code.


boolean flag = true; //assumes that 9 is indeed prime, unless proven otherwise





int c=0; //don't mind this... just an initialization





for(c=2;c%26lt;i;c++) //for numbers 2 to 8, we try the following





if((i%c)==0) //is i divisible by c? that is, if we divide i, which is 9 by c, at first 2... would the remainder be 0?





flag=false; //if the remainder is zero, this is not prime!


break; //so we quit the loop





else flag=true; //not really necessary





//....after the loop





if(flag) System.out...(Prime no) //It is a prime, state it


else ...// It is not a prime state it.





------





if i = 9, here's how it will go...


c = 2 // (i%c) == 0 will not be true since: 9%2 == 0, 1 == 0, which is false so go on with the loop


c = 3 // (i%c) == 0 will be true since: 9 % 3 == 0, 0 == 0, thus we mark the prime-ness of 9 (flag) as false, and we quit.





we then print the result


No comments:

Post a Comment