Thread: Prime Prime Prime Help Help Help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,666
    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
    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.

  10. #10
    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?

  11. #11
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    Quote Originally Posted by mltngpot
    all prime numbers (except 2) are prime, once again wasting precious processes.
    shouldn't that read, all prime numbers, other than 2, are odd. ?

    so testing for if it is odd, then seeing if it has ANY divisors other than 1 and itself is all you need to do. [ test for division by 2, 3, 5 ]
    iand non prime number with divide by one or more of these three.
    not all odd number have a square root that is an integer. ie: 15 is not prime, yet does not square root either to an integer. so testing for a square root is not going to result in a prime number.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  12. #12
    Registered User mltngpot's Avatar
    Join Date
    Jan 2006
    Posts
    5
    sorry, not thinking, all prime numbers are odd except two.
    and no prime number will have an integer sqrt, however, getting the sqrt of the number before hand will eliminate checking the sqrt multiple times. any program like this, is going to be run (most likely) using large numbers, since the small ones are readily available and the math is easy. 2, 3, 5, 7, 11.... so the amount of processes run cuts down signifigantly.

    if you had say
    Code:
      sqrtnum = sqrt(iNum);
      for (count = 0; count < sqrtnum; count++)
    you would cut down on the execution of the sqrt. you could even make it faster, by checking to see if sqrtnum was an integer before the loop, so in the case of iNum being 100, the sqrt would be 10, thus skipping the loop. and making the function exponentially faster. in this case, it would skip running the loop 10 times, (or 4 times faster if you only check against prime numbers), and if you wanted to get really smart, you would do a check against 2 and 5 also before the loop, and only check numbers ending in 1, 3, 7,9 because if its even its composite as well as if it ends in 5.

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Which rule restricted answering to this thread? 5?
    We should see the goal of the rule. It wants to prevent dead topics to come on first page. Answering to an unanswered thread is different. For example maybe I have had asked a very hard question that nobody could answered, but 5 months later somebody came and answered it. You think it is illegal? Of course it is not. It is the goal of the forum.
    Last edited by siavoshkc; 01-20-2006 at 01:12 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

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