Thread: Prime Prime Prime Help Help Help

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    Question Prime Prime Prime Help Help Help

    hi everyone, i have being working on this code for a long time.. but couldnt make it right

    it is simple find the prime number from the user inputs

    this is the code ....

    Code:
    bool IsPrime(int iNum)
    {
    // Check if i is less than square root of iNum (and cast iNum to type double)
        for (int i=2; i<=sqrt((double)iNum); i++) 
        {
            if (iNum == ((int)iNum/i)*i)// (inum%i == 0)		
            return false;
        }
        return true;
    }
    but this is not the exact code that i want ...

    my aim is to find the prime number that the user will input the max number,,,

    example here:

    input: 4
    output will be: not prime..

    please help ...... i know there are lot of related topic but .... still couldnt help.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    std::cout << (IsPrime(Input) ? "prime" : "not prime") << std::endl;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    try this instead of that code you got there.

    Code:
    inline bool IsPrime(long& n)
    {
       return((n & (n - 1)) == 0);
    }
    Last edited by Mitsukai; 01-20-2006 at 09:12 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Wow - first thread bumping, and then posting something which plainly isn't an anwer.
    Feel proud!.

  5. #5
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    im just pointing out something that is better. and it was not thread bumping... and i dont like you being rude to me...
    im trieing to help him out by telling him something thats faster and easier and you bash me.
    congrats.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> and it was not thread bumping...

    From the board rules. You did read the board rules didn't you? I added the bold font to help you see.
    5. Don't bump threads. (Bumping: posting messages on threads to move them up the list or to post on a thread that has been inactive for two weeks or longer).
    In what way was replying to a long dead thread from August 2004 not thread bumping? I'm intruiged.

    Reporting mods for picking you up on your rule breaking is not going to help you.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    how is posting something older than 2 weeks bumping a thread? where is the common sense in that. if you just reply to get it up k, or if the question was already answered and there is nothing to discuss ok. but clearly im forced to leave forums again bye..

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The common sense is that a questioner asking a question here 18 months ago is not likely to be sitting waiting for an answer still. He will have found an answer somewhere else or solved it himself.

    If you posted a very basic question 18 months ago, would you still be looking for an answer?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    you seriously have no common sense. wow. cant you think off that some poeple might still be looking at that thread? someone even redicted to that thread. someone might have find it usefull what i posted. do you really think the forums are gonna get any better by that fked up rule . i dont think so. im gonna enjoy my time at codeguru alot more than here.

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by Mitsukai
    you seriously have no common sense. wow. cant you think off that some poeple might still be looking at that thread?
    Those are the rules...

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >try this instead of that code you got there.
    Apparently you didn't.

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    im gonna enjoy my time at codeguru alot more than here.
    I think we're going to enjoy your time there a lot more than here, too.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Yes, answering the old not answered(but good) questions is a good idea.

  14. #14
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    There are reasons for the rules here. They have been developed over many years, by a lot of very experienced people, and for the most, they work. We will not be changing them anytime soon.

    >>> im gonna enjoy my time at codeguru alot more than here.

    If you approach codeguru, (where many of us are also regulars of course), the way you approached here, I somehow doubt it.

    Feel free to ignore this advice, but the net is a big bad ugly place, where people are quick to flame, and far more mercilessly then you have ever been shown here.

    If you have been upset by the way you were treated here, you really need to consider an alternative strategy for your learning process.

    Good luck.

    *** No more off topic in this thread now please. Adrian...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  15. #15
    Registered User mltngpot's Avatar
    Join Date
    Jan 2006
    Posts
    5
    1) your repeatedly checking the sqrt of iNum, do it once, before the loop, your wasting precious processing power.
    2) your checking all numbers, even and odd, all prime numbers (except 2) are prime, once again wasting precious processes.
    3) your comment is better written than your if statement itself
    4) Isn't there a rule about not having other people do your homework?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:06 PM
  2. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 04:32 PM
  4. Prime Wonder
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-07-2002, 11:49 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM