Page 1 of 1

Boolean help?

Posted: Thu Feb 25, 2010 7:58 am
by MadPumpkin
Alright I just need help doing this, I really don't know how to go about writing this program... And if someone might be able to help that'd be awesome.
I was just attempting it in the shell not a graphics API or anything.

Alright so I have 200 chests and 200 pirates, and one other guy named blackbeard, and I dunno lets say it's blackbeards birthday. Alright so the pattern is like this, the first pirate opens every single chest. The second pirate goes and changes the position(if it's open he closes it, if it's closed he opens it) of every OTHER chest (skips 1 each). The third pirate goes and changes the position of every 3 chests (skips 2 each time). And so on and so forth.
after ALL 200 pirates have gone through the chests, blackbeard goes through and takes the treasure out of ONLY the ones that are open. WHICH did he take from?

I no longer have the source code for this because it was just me quickly trying to do it at school, but when I was trying to figure this out, I realized that I didn't really know enough about Boolean functions... Which was how I was trying to do it.

Re: Boolean help?

Posted: Thu Feb 25, 2010 12:08 pm
by nardi11011
Fuck, I wish there were more questions like this on Stack Overflow. Let's first solve the problem before getting to the code.

200 chests, 200 pirates.
First pirate opens all.
Second changes position of every second chest.
Third changes position of every third chest.

Which could also be read as:

200 booleans, 200 pirates.
First pirate changes position of every boolean.
Second changes position of every second boolean.
Third changes position of every third boolean.

But now, how do we run this in code easily? If we change the wording a bit, it becomes a lot simpler:

200 booleans, 200 pirates.
First pirate changes position of every boolean whose number is divisible by one.
Second changes position of every boolean whose number is divisible by two.
Third changes position of every boolean whose number is divisible by three.

Or, more universally:
n-th pirate changes position of every boolean whose number is divisible by n.

Now, there are two things you need to know to implement this: how to check if x is divisible by y, and how to set the value of a boolean to whatever it is currently not.

The first is done using the modulus operator (%). "x % y" gives you the remainder of the nearest whole multiple of x. So 23 % 10 would return 3, as 20 / 10 == 2, and 23 - 20 = 3. So if x is divisible by y, x % y will be equal to 0. The second is done using the not operator (!). As you may have guessed, !bool returns whatever value bool is currently not. So !true == false, and !false == true.

Now let's get to the code! It's written in C#, but the important stuff is identical in C/++.

Code: Select all

static void Main(string[] args)
{
    bool[] chests = new bool[200];

    for (int numPirate = 1; numPirate <= 200; numPirate++)
    {
        for (int numChest = 1; numChest <= 200; numChest++)
        {
            if (numChest % numPirate == 0)
                chests[numChest - 1] = !chests[numChest - 1]; //Arrays are zero-indexed!
        }
    }

    Console.WriteLine("HARRRR! I gots me some treasure from:");

    for (int numChest = 0; numChest < 200; numChest++)
    {
        if (chests[numChest] == true)
            Console.WriteLine("Chest {0}", numChest + 1);
    }

    Console.ReadLine();
}

Re: Boolean help?

Posted: Thu Feb 25, 2010 7:46 pm
by MadPumpkin
Oh my god, THANK you you've been a huge help! Maybe now I can actually get the part that was broken of Repo's source fixed too... Thank you again!

Re: Boolean help?

Posted: Thu Feb 25, 2010 8:37 pm
by davidthefat
Gah my head hurts...

Re: Boolean help?

Posted: Fri Feb 26, 2010 8:20 am
by The Great
This reminds me of the 1000 lockers problem that was given as an assignment in my java class. :lol:
Google 1000 lockers problem if you want to know more.

Re: Boolean help?

Posted: Fri Feb 26, 2010 7:32 pm
by ClassA
The Great wrote:This reminds me of the 1000 lockers problem that was given as an assignment in my java class. :lol:
Google 1000 lockers problem if you want to know more.
Yeah, I was asked to do this at one point I believe - this is also the kind of programming/logic problem which is often given during programming interviews.

Re: Boolean help?

Posted: Thu Mar 04, 2010 4:26 am
by MrDeathNote
ClassA wrote:
The Great wrote:This reminds me of the 1000 lockers problem that was given as an assignment in my java class. :lol:
Google 1000 lockers problem if you want to know more.
Yeah, I was asked to do this at one point I believe - this is also the kind of programming/logic problem which is often given during programming interviews.
Had that one myself, didn't turn out to be so bad...