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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

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

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question 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. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,163
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    kinkita (July 16th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Check if number is prime number or not problemm

    Quote Originally Posted by KevinWorkman View Post
    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 ---

    Quote Originally Posted by robin_ View Post
    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. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    Remember you also only have to check the odd divisors.

  7. #6
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

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

    That shall be the next step

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,163
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by KevinWorkman View Post
    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. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,163
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

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

    Quote Originally Posted by robin_ View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by KevinWorkman View Post
    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

Similar Threads

  1. determining a prime number
    By ob7ivion in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2013, 09:27 AM
  2. Prime number solver.
    By Danny123 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: June 4th, 2012, 05:30 AM
  3. display all prime factors of a number
    By mia_tech in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 18th, 2012, 06:55 PM
  4. Prime Number Code Help!
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 6th, 2012, 11:07 PM
  5. Prime Number Program for class
    By chachunga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2011, 12:05 AM

Tags for this Thread