Thread: divisibility by 3

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    I don't think he's starting with chars which is where you two lost each other, I think he is starting from an integer and not necessarily an input from the command line. So what he need is a general purpose function that takes an int and returns if it's divisible by 3 and therefore was stating it would be difficult to parse the individual digits without using / or %
    Ah, that makes sense.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Tropical Coder
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    For those of you that follow the contest forum I submit this:
    Code:
    bool divby3(int num)
    {
         int dividend = (double)num * .3333 + .1;
         int remainder = num-(dividend * 3);
         return remainder == 0;
    }
    For those that don't follow the contest forum, this is not a serious answer(though it works).
    Last edited by Darryl; 06-29-2007 at 03:19 PM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    I think that operator * is like / and %, so if this is a homework question as I suspect, Brij cannot use it anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    If you have an efficient operation for counting the number of nonzero bits in an unsigned int (like some processors do) you can use

    Code:
    int divisible3(unsigned int x) {
    
      do {
        x = abs(countbits(x & 0xAAAAAAAA) - countbits(x & 0x55555555)); 
      } while (x > 3);
    
      return x == 0;
    }
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  5. #20
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Quote Originally Posted by Darryl View Post
    For those of you that follow the contest forum I submit this:
    Code:
    bool divby3(int num)
    {
         int dividend = (double)num * .3333 + .1;
         int remainder = num-(dividend * 3);
         return remainder == 0;
    }
    For those that don't follow the contest forum, this is not a serious answer(though it works).
    Sorry mate,
    Code:
    3003 * .3333 = 1000.8999
    + .1 = 1000.9999
    * 3 = 3002.9997
    Quote Originally Posted by laserlight View Post
    I think that operator * is like / and %, so if this is a homework question as I suspect, Brij cannot use it anyway.
    If this is a homework assignment, it probably is assigned because there's so many different ways to solve it. I don't see how solving it with multiplication is really less valid than bit counting.
    Callou collei we'll code the way
    Of prime numbers and pings!

  6. #21
    Tropical Coder
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    [QUOTE=QuestionC;654440]Sorry mate,
    Code:
    3003 * .3333 = 1000.8999
    + .1 = 1000.9999
    * 3 = 3002.9997
    /QUOTE]

    I can always add more 3s to extend its useful range.
    Plus, it was only meant as a joke based on the contest thread I linked to.

  7. #22
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Speaking of counting the number of bits in an unsigned int, I found this interesting bit of speculation. http://www.moyogo.com/blog/2005/09/secret-opcodes.html
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

Popular pages Recent additions subscribe to a feed