DevPinoy.org
A Filipino Developers Community

>>> First two to make 3 wins! <<<

Short Quizzes

rated by 0 users
This post has 15 Replies | 4 Followers

Top 10 Contributor
Posts 461
Points 6,935
Orange Posted: 12-06-2006 12:44 AM
Feel free to use your own language.
 
Peter the Postman is assigned to night duty at the Post Office. Peter gets easily bored, so he decides to amuse himself as follows:

1. The are 100 post office boxes at his assigned substation.
2. First Peter goes through and opens all 100 mailboxes.
3. He then returns to the first box and closes every other one.
4. Then he returns to the first box and checks every third one:
• If it’s open, he closes it
• If it’s closed he opens it
5. He repeats this process for every fourth box, then every fifth box, etc.
until he gets through them all.
6. At the end, which boxes are open and which are closed?

Now the teacher said that an array and a nested for loop would be needed.
 
--------------------------------------------------------------------------------------
 
Another, by searching the web, I found a string permutation problem... which until now I cannot solve. Damn! anyways, guess what I found something related. It's fun.
 
 
...Think 64 Bit
  • | Post Points: 65
Top 10 Contributor
Posts 1,969
Points 39,350
Orange:
Feel free to use your own language.
 
Peter the Postman is assigned to night duty at the Post Office. Peter gets easily bored, so he decides to amuse himself as follows:

1. The are 100 post office boxes at his assigned substation.
2. First Peter goes through and opens all 100 mailboxes.
3. He then returns to the first box and closes every other one.
4. Then he returns to the first box and checks every third one:
• If it’s open, he closes it
• If it’s closed he opens it
5. He repeats this process for every fourth box, then every fifth box, etc.
until he gets through them all.
6. At the end, which boxes are open and which are closed?

Now the teacher said that an array and a nested for loop would be needed.
 
--------------------------------------------------------------------------------------
 
Another, by searching the web, I found a string permutation problem... which until now I cannot solve. Damn! anyways, guess what I found something related. It's fun.
 
 

hahaha! i know this one. I think this was a machine problem back when i was in college.

devpinoy sig

  • | Post Points: 20
Top 10 Contributor
Posts 1,100
Points 17,955
this is one of the good math puzzles that i taught in my summer class, just a simple analysis of numbers will do, language of divisibility and factors would help
  • | Post Points: 20
Top 10 Contributor
Posts 1,969
Points 39,350

abah! walang sumagot!

The first person to give the best solution(cleaness code, memory use, fewest lines of code) for this problem gets a new free book :)

GO!

devpinoy sig

  • | Post Points: 20
Top 10 Contributor
Posts 953
Points 22,750
tanong po!

in #3, does he also close the first box? or only every other one after that (which means he closes every other box starting from the THIRD box)?
similarly in #4, does he toggle the state of the first box? or only every third one after that (which means he toggles the state of every third box starting from the FOURTH box)?
when does the process end? When he has toggled the states of ALL boxes? I figure that all boxes would have been toggled at least once after the tenth pass.
http://devpinoy.org/blogs/cruizer
Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
  • | Post Points: 20
Top 10 Contributor
Posts 1,100
Points 17,955
cruizer:
tanong po!

in #3, does he also close the first box? or only every other one after that (which means he closes every other box starting from the THIRD box)?
similarly in #4, does he toggle the state of the first box? or only every third one after that (which means he toggles the state of every third box starting from the FOURTH box)?
when does the process end? When he has toggled the states of ALL boxes? I figure that all boxes would have been toggled at least once after the tenth pass.

good question, mejo tagilid nga yata english on where the postman starts his toggling.  if this is similar to the classroom door problem, for #3, he starts toggling on every other one after the first box meaning all the other even numbered boxes.  same with #4, he toggles starting from 3 to all  multiples of three. 

in that case the doors are toggled if the number of pass of the postman is a factor of the door number.  door #6 will be toggled by the first, second, third and sixth pass.  what's left open are all perfect square numbered doors since they are the ones with odd number of divisors.  it is because in number theory, a number expressed in its prime factorization (a^x .b^y.c^z) has  a number of divisors equal to (x+1)(y+1)(z+1), formula derived from the permutations of the factors including a factor raised to 0 (x^0) which is just 1.  The said expression will  only be odd if all x, y, and z are even which only happens when the number is a perfect square.  so my code is below, excluding the math analysis para shorter, hahaha:


   37         public List<int> GetOpenDoors()

   38         {

   39             List<int> openDoors = new List<int>();

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

   41             {

   42                 double squareRoot = System.Math.Sqrt(i);

   43                 if (System.Math.Ceiling(squareRoot) == squareRoot) openDoors.Add(i); //no remainder

   44             }

   45             return openDoors;

   46         }

  • | Post Points: 20
Top 10 Contributor
Posts 461
Points 6,935

jokiz:
 

   37         public List<int> GetOpenDoors()

   38         {

   39             List<int> openDoors = new List<int>();

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

   41             {

   42                 double squareRoot = System.Math.Sqrt(i);

   43                 if (System.Math.Ceiling(squareRoot) == squareRoot) openDoors.Add(i); //no remainder

   44             }

   45             return openDoors;

   46         }

WOW!!! Indifferent

Here's mine;

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string mailbox[1000];    // <-- I make it a thousand, kinakapos eh. hehehe.
  for(int temp = 1; temp <= 100; temp++)
    mailbox[temp] = "Open";
  
  int counter = 2;
  while(counter <= 100)  {
    int x = 1;
    
    while(x <= 100)  {
      if(mailbox[x] == "Close")
        mailbox[x] = "Open";
      else
        mailbox[x] = "Close";
      x = x + counter;
    }

  for(int temp = 1; temp <= 100; temp++)
    cout << "Mail Box No. " << temp << " is " << mailbox[temp] << endl;  

  counter++;
  system("PAUSE");  
  }

  system("PAUSE");
  return 0;

}

...Think 64 Bit
  • | Post Points: 5
Top 25 Contributor
Posts 90
Points 1,280
// Here's mine...

            int[] boxes = new int[100];
            for (int skip = 0; skip<boxes.Length; skip++)
            {
                for (int i = skip; i < boxes.Length; i += skip + 1)
                {
                    boxes[i ] = boxes[i ] == 1 ? 0 : 1;
                }
            }

// boxes with value of 1 are the open mailboxes, those with 0 are closed... or to put this in code:

            for (int j = 0; j < boxes.Length; j++)
                Console.WriteLine("Mailbox #{0} is {1}", j + 1, boxes[j] == 1 ? "Open" : "Closed");


// we can also use bool (or string) for our data type, we just have to toggle its state everytime we're at that door.  using bool is easier to toggle.
  • | Post Points: 20
Top 10 Contributor
Posts 753
Points 10,150
Here's mine, but I'm not sure if its correct... I just tried. Walang tatawa! Zip it!
//I hope this is correct...
var box = new Array();
var total=100;
var batman; //palayaw ng isa kong co-employee :D

for (i=1; i<=total; i++) {
    batman = i % 2;
    if (batman!=0) {
        box[ i ]="Close";
    } else {
        box[ i ]="Open";
    }
}
for (i=4; i<=total; i++) {
    for (g=i; g<=total; g++) {
        if (box[ g ]=="Open") {
            box[ g ]="Close";
        } else {
            box[ g ]="Open";
        }
    }
}

for (i=1; i<=total; i++) {
    document.write("<span style=\"font-family:courier new;font-size:8pt;\">");
    if (box[ i ]=="Open") {
        document.write("<span style=\"color:#4F8622;\">");
    } else {
        document.write("<span style=\"color:#B41717;\">");
    }
    document.write("Box #" + i + " is " + box[ i ]);
    document.write("</span></span><br>");
}


  • | Post Points: 5
Top 10 Contributor
Posts 461
Points 6,935
keithrull:

abah! walang sumagot!

The first person to give the best solution(cleaness code, memory use, fewest lines of code) for this problem gets a new free book :)

GO!

What book? I'll do my best if it is a "kama sutra". Super Angry

...Think 64 Bit
  • | Post Points: 20
Top 150 Contributor
Posts 7
Points 105

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
namespace GothMilk {
class Program {
static void Main(string[] args) {

//1. The are 100 post office boxes at his assigned substation.
const int TOTAL_BOX = 100;
string[] boxState = new string[TOTAL_BOX];

            //2. First Peter goes through and opens all 100 mailboxes.
//this means that all boxes are CLOSED. hehehe. Now lets OPEN it.
for (int index = 0; index < TOTAL_BOX; index++) {
boxState[index] = "OPEN";
}

            //di ko magets yung tanong talaga. sana ma re-phrase.
//but anyway here's my attempt.
for (int alpha = 0; alpha < TOTAL_BOX; alpha++) {
for (int omega = alpha; omega < TOTAL_BOX; omega++) {
boxState[omega] = boxState[omega] == "OPEN" ? "CLOSE" : "OPEN";
}
System.Console.WriteLine("Box #{0} is {1}", alpha+1, boxState[alpha]);
}


//System.Console.Read();

}
}
}

  • | Post Points: 5
Top 75 Contributor
Posts 14
Points 250

 

Hi  guys i know that this is an old thread but i was new here and i just stumble this trivia ...and it seems that it is still unsolved so hhhmm i just thought to give it a try:

All i can see with the problem is all about arithmetic progression.. 

Here is my code:

    Sub Main()

        Dim Boxes(99) As Boolean

        Dim process, skip As Integer

        Dim mySkip As Integer = 0

        Dim check As Integer = 0

 

 

        '1.initialize this is actually the first process

        For box As Integer = 0 To 99

            Boxes(box) = True

        Next

 

      

 

        '2. second process and so on...

        For process = 1 To 99

            skip = process

            mySkip = 1

            check = 0

 

            Do

                mySkip += 1

                '(using the arimetic progression to determine the next        nth index to check)

                check = 1 + (mySkip - 1) * (skip) 'formula for progression is An=A1 + (N-1)D

                If check < 100 Then

 

                    Boxes(check - 1) = Not Boxes(check - 1)

 

 

                Else

                    Exit Do

 

                End If

            Loop

            'print the filnal state of the Box(process)

            Console.WriteLine("Box#{0} is {1}", process + 1, Boxes(process))

 

        Next

 

        Dim counter As Integer

        For Each box As Boolean In Boxes

            If box Then

                counter += 1

            End If

        Next

        '3.print the final answer to how many boxes are open  for  cross-checking 

        Console.WriteLine("Boxes that are open:" + counter.ToString)

 

    End Sub

 

my code gives me  a total open boxes of  91 ..

 I tried to check other offered solution  but  it seems  nothing tallysConfused

so i have to simulate this to excel using some macros ... well i'm conviced that this works.. the final state of boxes concur with my programStick out tongue

of course i might be correct if i understand the problem correctly so

here is the link of the excel  for checking..thanksSmile

www.numonix.net
  • Filed under:
  • | Post Points: 5
Top 25 Contributor
Posts 192
Points 3,255

This looks like a variation of the Sieve of Eratosthenes - an algorithm for finding prime numbers.

If you are interested in other implementations, visit Ward's Wiki

[jop]

  • | Post Points: 20
Top 75 Contributor
Posts 14
Points 250

 Hi jop! I checked that sieve of erastosthenes ... very clever aside from the fact its too old! I havent really heard it but now you mentioned it i got pretty excited you keep the ball rolling here!.. it really works as a sieve.. sieving  prime numbers in every process of iteration.. Hmm but back to PETER POSTMAN problem we are not really looking for the prime numbers ...but its kinda related alright! actually i  saw  a new  solution to this here- is my theory/propositon:

" If you can tell me how many times did PETER the postman touched the Nth mail boxes(say  box#82 ) then you can tell me its final state!"

Well i leave the solution for you guys... but i'l post mine next week! This time you wont be needing array...actually i think i got a working solution already and  you would be using lesser code.  Thats EXtend the trivia! I wonder where is orange now?


 

www.numonix.net
  • | Post Points: 5
Top 75 Contributor
Posts 14
Points 250