# 100 doors exercise code using C#

Can you please check my code if it is correct for the problem 100 doors?

This is the problem:

You have 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, you visit every door and toggle the door (if the door is closed, you open it; if it is open, you close it). The second time you only visit every 2nd door (door #2, #4, #6, ...). The third time, every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door. Question: What state are the doors in after the last pass? Which are open, which are closed?

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

bool test = true;
bool temp1 = false;
bool temp2 = true;
bool temp;

for (int i = 1; i <= 100; i++) {

if (i == 3) {
temp = temp1;
temp1 = temp2;
temp2 = temp;
}

if (test == true) {

for (int a = 1; a <= 100; a++) {

if (a % i == 0) {

doors[a - 1] = temp1;
}
else if(a % i != 0)
doors[a - 1] = temp2;
}
}
else if (test == false) {

for (int c = 0; c < 100; c++) {

doors[c] = true;

}
test = true;
}
}
for (int d = 0; d < 100; d++) {
Console.WriteLine(doors[d]);
}

}
}


I have no idea what this does:

if (i == 3) {
temp = temp1;
temp1 = temp2;
temp2 = temp;
}


Please don't say if (test == true) or if (test == false)

but just if (test) or if (!test)

and you can just say else instead else if (test == false)

    bool[] doors = new bool;
for (int step = 1; step <= 100; ++step) {
for (int door = step; door <= 100; door += step) {
doors[door] = !doors[door];
}
}

for (int door = 1; door <= 100; ++door) {
if (doors[door]) Console.WriteLine(door);
}


You'll see that the output is all the square numbers: 1, 4, 9, 16, 25, 36, 49, 64, 81 and 100

• Thank you for checking but is my code correct? Feb 4, 2015 at 10:18
• I ran it, and it says only 99 is true, so it is not correct. ideone.com/VHHlai Feb 4, 2015 at 10:21
• Thank you sir but how come the output is like that? Feb 4, 2015 at 10:30
• Every number except squares have an even number of divisors. Take for example 12: 12 = 1x12, 2x6 and 3x4, so its divisors are 1,2,3,4,6 and 12. That means door 12 will be toggled on iterations 1,2,3,4,6 and 12. It will be toggled an even number of times, so it will end up closed. Square numbers have an odd number of divisors, because they have a 'double'. 16 = 1x16, 2x8 and 4x4. 4 is a double divisor. Door 16 will be toggled an odd number of times and will end up opened. Feb 4, 2015 at 10:42

A construct like

if (bool == true)


can be simplified to

if (bool)


and in addition if you have a if..else if like

if (bool == true)
{

}
else if (bool == false)
{

}


you can omit the else if and replace it with a else.

You should name your variables in a meaningful way, so that you or Sam the Maintainer will know at first glance what they are about.

bool test = true;
bool temp1 = false;
bool temp2 = true;
bool temp;


Placing this whole code inside main() can be considered as bad practice. You should separate the responsibilities like toggling doors and printing the result.

For toggling a boolean value, you can simply do booleanVariable = !booleanVariable;

You have some unneeded vertical space (new lines) in your code which should be removed for readability.

Also there is no convention for where to place an opening brace { you should at least stick to your style. Right now you are mixing where you place the brace.

After applying these points your code will look like

class Program
{
static void Main(string[] args)
{
Program p = new Program();
bool[] toggledDoors = p.SolveDoorExcercise(100);
String output = p.FormatOutput(toggledDoors,true);
Console.Write(output);
}

private bool[] SolveDoorExcercise(int numberOfDoors)
{
bool[] doors = new bool[numberOfDoors];
for (int step = 1; step <= numberOfDoors; ++step)
{
for (int i = step - 1; i < numberOfDoors; i += step)
{
doors[i] = !doors[i];
}
}
return doors;
}

private string FormatOutput(bool[] doors, bool opened)
{
StringBuilder sb = new StringBuilder(doors.Length * 2);
for (int i = 0; i < doors.Length; i++)
{
if (doors[i] == opened)
{
sb.AppendLine((i + 1).ToString());
}
}
return sb.ToString();
}
}