# Check if number is prime number or not problem[SOLVED]

• July 16th, 2013, 11:23 AM
kinkita
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:

Code :

``` 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?
• July 16th, 2013, 11:42 AM
KevinWorkman
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.
• July 16th, 2013, 01:20 PM
robin_
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. :)
• July 16th, 2013, 01:59 PM
kinkita
Re: Check if number is prime number or not problemm
Quote:

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 :D. Now the method looks like this:
Code :

``` 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 ---

Quote:

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 :)

Quote:

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
• July 16th, 2013, 02:18 PM
robin_
Re: Check if number is prime number or not problem[SOLVED]
Remember you also only have to check the odd divisors.
• July 16th, 2013, 03:14 PM
kinkita
Re: Check if number is prime number or not problem[SOLVED]
That shall be the next step :)
• July 17th, 2013, 07:38 AM
KevinWorkman
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.
• July 17th, 2013, 10:05 AM
robin_
Re: Check if number is prime number or not problem[SOLVED]
Quote:

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?
• July 17th, 2013, 10:41 AM
KevinWorkman
Re: Check if number is prime number or not problem[SOLVED]
Quote:

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. :D
• July 17th, 2013, 10:59 AM
robin_
Re: Check if number is prime number or not problem[SOLVED]
Quote:

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. :D

I see, sorry didn't mean to spoil any ideas :D