Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: Check if number is prime number or not problem[SOLVED]

1. Check if number is prime number or not problem[SOLVED]

Hi everyone!

I've tried to develop a simple programm which will tell the user if the given number is a prime number or not.

this is the method I've been trying to fix:

```    public void checkIfPrime(Double d){
Double TestTo = d;
for(int x=1; x<TestTo; x++){
if(d / x == 1.0 && foundDivs == 0){
primeNoYes = "yes";
foundDivs++;
}
else
{primeNoYes = "no";}

}

}```

What I'm basically trying to do is to calculate how many numbers the given number can be divided with that equals 1. Since a prime number only has two numbers it can be divided by, which is 1 and itself. As you can see I'm ignoring the 1 for now as it's unnessisary, i think.

and then the programm gives a message box that says "Yes if the number onlt has one number divided by which gives 1 and reverse.

At least that what I thought would work, but it didn't...

Any help, what would you do?

2. Re: Check if number is prime number or not problemm

When you say it didn't work, what exactly do you mean? What exactly does this program do? Hard code it into an SSCCE with some numbers that result in the broken behavior.

Also, take a step back and look at what you're doing. You're checking to see how many numbers divide into another number 1 time. That answer will only ever be 1, since the only division that equals 1 is a number over itself.

Also, your if statement is rather wonky- what exactly are you trying to accomplish with it? I recommend tracing through it with a debugger, or at least adding a bunch of print statements, to better understand what this code is doing.

3. The Following User Says Thank You to KevinWorkman For This Useful Post:

kinkita (July 16th, 2013)

4. Re: Check if number is prime number or not problemm

Instead of counting the divisors, have you tried checking all possible divisors? I mean iterate from 2 to (n-1) if your number is n. A tip by the way, if your number is even and larger than 2, you don't have to count divisors at all since 2 is the only even prime number.

I'll help you all the way through if needed, but I believe one learns best from trying on their own.

5. Re: Check if number is prime number or not problemm

Originally Posted by KevinWorkman
Also, take a step back and look at what you're doing. You're checking to see how many numbers divide into another number 1 time. That answer will only ever be 1, since the only division that equals 1 is a number over itself.
You're refering to this code right? "if(d / x == 1.0 && foundDivs == 0)"; How could i not see this -_-. I took this code from an example:
d % x == 1.0
and if it's true then d would be an even number, but it didn't, I have no idea why but I changed "%" to "/".

I did what you said and took another look at it, and I did it . Now the method looks like this:
```    public void checkIfPrime(Double d){
Double TestTo = d;
if(d != 1)
{
for(int x=1; x<TestTo; x++){
if(!(((d/x)%(1.0))>0.0)){
foundDivs++;
//System.out.println(d+" divided with "+ x);
//System.out.println(d +" got "+foundDivs);
}
}
}
else
{foundDivs++;}
}```

Maybe not the fastest piece of code, but it works

Thank you

--- Update ---

Originally Posted by robin_
Instead of counting the divisors, have you tried checking all possible divisors? I mean iterate from 2 to (n-1) if your number is n. A tip by the way, if your number is even and larger than 2, you don't have to count divisors at all since 2 is the only even prime number.
I tried that after KevinWorkman's post but I think I got a sulotion which works as as well, thanks though

I'll help you all the way through if needed, but I believe one learns best from trying on their own.
I agree on that but a push in the right direction is good too

6. Re: Check if number is prime number or not problem[SOLVED]

Remember you also only have to check the odd divisors.

7. Re: Check if number is prime number or not problem[SOLVED]

That shall be the next step

8. Re: Check if number is prime number or not problem[SOLVED]

You also only need to check up to n/2, where n is the number you're checking for prime-ness.

9. Re: Check if number is prime number or not problem[SOLVED]

Originally Posted by KevinWorkman
You also only need to check up to n/2, where n is the number you're checking for prime-ness.
Why n/2? Don't you mean the square root of n?

10. Re: Check if number is prime number or not problem[SOLVED]

Originally Posted by robin_
Why n/2? Don't you mean the square root of n?
Stopping at n/2 is the natural next improvement to code that goes all the way to n. From there you can go the next step of stopping at square root of n, and from there you can do even smarter things!

Primality test - Wikipedia, the free encyclopedia

My point was to get the OP thinking about that improvement path, not to offer the end-all be-all of primality checkers.

11. Re: Check if number is prime number or not problem[SOLVED]

Originally Posted by KevinWorkman
Stopping at n/2 is the natural next improvement to code that goes all the way to n. From there you can go the next step of stopping at square root of n, and from there you can do even smarter things!

Primality test - Wikipedia, the free encyclopedia

My point was to get the OP thinking about that improvement path, not to offer the end-all be-all of primality checkers.
I see, sorry didn't mean to spoil any ideas