Where did I make a mistake ?

Here is the question:

Write a method called isPrime that takes one integer as a parameter and returns

true if the parameter is a prime number, or false otherwise. A prime number is

a number whose only divisors are 1 and itself. There are complex algorithms for

determining whether a number is a prime or not, but we will ask you to implement

the simplest algorithm, which is to simply test every possible divisor to check if

there are any divisors other than 1 or the number itself.

Here is my code :

publicstaticboolean isPrime (int number )

{

boolean answer;

int count = 2;

while ( count < number )

{

answer = isDivisor( count, number );

if ( !answer )

{

answer =true;

count++;

}

else

{

answer =false;

break;

}

}

return answer;

}

[1556 byte] By [Ace1a] at [2007-11-27 5:19:32]
# 1

the isDivisor method :

public static boolean isDivisor ( int m, int n )

{

boolean result;

int check1;

double check2;

check1 = n / m;

check2 = (double)n / m;

if ( check2 == check1 )

{ result = true; }

else

{ result = false; }

return result;

}

Ace1a at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 2
What specific problem are you having?
jverda at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 3

Here is the psuedocode for a prime method, which is cleaner that your solution.

loop from 2 to square root of number {

if number is divisible by current divisor {

(you can use the mod operator here)

return false

}

}

return true

Note there are other better solutions but this should do for your level.

floundera at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 4
The problem is that when click compile, it says the variable answer may not have been initialized.
Ace1a at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 5

> The problem is that when click compile, it says the

> variable answer may not have been initialized.

That's because it's possible for it to get to the return answer line without you ever setting answer--if the body of your while loop is never entered.

So you need to figure out under what conditions that might happen, decide whether it should return true or false in that case, and then initialize answer to that value when it's declared.

jverda at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 6
Then initialize it. This means give it an initial value as it is possible to follow a path through your code where it does not get assigned a value before you try use it.
floundera at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 7
what is your answer supposed to be for number == 2?
petes1234a at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 8
Should I consider negative values? How should I improve my code?Thanks
Ace1a at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 9
> what is your answer supposed to be for number == 2?nevermind, my mistake.
petes1234a at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 10

> Should I consider negative values?

That's up to you.

> How should I

> improve my code?

By defining clearly how it will behave in all cases, writing it to follow that definition, getting it to compile, and then testing and debugging until it gives the proper results.

jverda at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 11

> Should I consider negative values?

No. Primes are taken to be positive, and greater than 1, for that matter.

> How should I improve my code?

Reduce isDivisor to one line of code. Do not use double.

When you test if n is a prime, you only need to test for factors up to sqrt(n),

because if n == a*b and a > sqrt(n), then b < sqrt(n).

You should also check out the Sieve of Eratosthenes:

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

[url #" style="display: block; background-image: url('http://upload.wikimedia.org/wikipedia/commons/9/94/Animation_Sieve_of_Eratosth.gif'); width: 445px; height: 369px] [/url]

Hippolytea at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 12
How to improve it ? It makes me mad...........
Ace1a at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 13

wait. If I don't have to consider negative value, as you said that prime numbers are taken to be positive and greater than 1, can I just initialize the boolean answer to true?

public static boolean isPrime ( int number )

{

boolean answer = true ;

int count = 2;

while ( count < number )

{

answer = isDivisor( count, number );

if ( !answer )

{

answer = true;

count++;

}

else

{

answer = false;

break;

}

}

return answer;

}

Ace1a at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 14
> and can I just initialize the> boolean answer to true?> Try it and find out?
petes1234a at 2007-7-12 10:42:58 > top of Java-index,Java Essentials,Java Programming...
# 15
If you implement my pseudocode then you don't need a boolean variable at all!
floundera at 2007-7-21 21:25:55 > top of Java-index,Java Essentials,Java Programming...
# 16
> How to improve it ? It makes me mad...........WTF?
jverda at 2007-7-21 21:25:55 > top of Java-index,Java Essentials,Java Programming...
# 17

> wait. If I don't have to consider negative value

What do you mean "not consider" them? Do you mean assume that method will never be called with a negative number, so don't worry about how it behaves in that case? That's one option. As I said, it's up to you and your requiements.

> you said that prime numbers are taken to be positive

> and greater than 1, can I just initialize the

> boolean answer to true?

It depends on what possible inputs you could have (your preconditions) and what the answer should be in that case.

jverda at 2007-7-21 21:25:55 > top of Java-index,Java Essentials,Java Programming...
# 18
> > Should I consider negative values? > > No. Primes are taken to be positive, and greater than> 1, for that matter.> So you are considering negative numbers, but their primeness is defined as being false ;)
jwentinga at 2007-7-21 21:25:55 > top of Java-index,Java Essentials,Java Programming...
# 19
> How to improve it ? It makes me mad...........you've been handed everything you need to know on a silver platter. If you still can't figure it out maybe you should try to get a degree in burgerflipping instead.
jwentinga at 2007-7-21 21:25:55 > top of Java-index,Java Essentials,Java Programming...