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 it5. 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. http://www.anagramgenius.com/server.html
hahaha! i know this one. I think this was a machine problem back when i was in college.
(x-a)(x-b)(x-c)...(x-z) Join MSPhil community
↑ Grab this Headline Animator
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!
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.
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 }
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!!!
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;
}
//I hope this is correct...
var box = new Array();var total=100;var batman; //palayaw ng isa kong co-employee :Dfor (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>");}
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".
12345678910111213141516171819202122232425262728
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(); } }}
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))
Dim counter As Integer
For Each box As Boolean In Boxes
If box Then
counter += 1
'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 tallys
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 program
of course i might be correct if i understand the problem correctly so
here is the link of the excel for checking..thanks
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]
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?
as i promised , using the above proposition the solution is:
Dim N, e, processCount As Integer
Console.Write("Enter the element number to check (2-100 only):")
' this is really tricky since this is hard to see ..
' when we are referring to the nth box we are actually offset by 1 in pacing this means
' box#5 is actually 4 pace away hence we are aftering factor of 4 not 5
' and also take note that when we say we have a factor 2 this is actually done in process#3
'..you'l see later
N = CInt(Console.ReadLine) - 1
processCount = 0 ' initial
For e = 1 To N 'this is it .. e=1 means its process#2
' to determine if e is a factor of N
If N Mod e = 0 Then
Console.WriteLine(CStr(e)) ' print factors for checking
processCount += 1
'now everytime peter process the box he invert the state so.. its closed-open process
'if you can imagine the sequence if element e is processed 3x then starting as OPEN!
' then closed(first factor which is actually 2nd process!)..open(2nd factor or 3rd process)
' it will end up in open(3rd factor) state. in other words if processcount is odd it means its closed
'ans if its is even number it is open!
'.. well that was as far as my imagination tells me since it is just open-closed state
'only = odd-even state.
' its just like: odd means open and even means closed
'Now even numbers is always divisible by 2
If processCount Mod 2 = 0 Then
Console.WriteLine("Box " + CStr(N + 1) + " is Open")
Console.WriteLine("Box " + CStr(N + 1) + " is Closed")
Thanks for reading.