r/dailyprogrammer • u/mattryan • Apr 05 '12
[4/5/2012] Challenge #36 [easy]
1000 Lockers Problem.
In an imaginary high school there exist 1000 lockers labelled 1, 2, ..., 1000. All of them are closed. 1000 students are to "toggle" a locker's state. * The first student toggles all of them * The second one toggles every other one (i.e, 2, 4, 6, ...) * The third one toggles the multiples of 3 (3, 6, 9, ...) and so on until all students have finished.
To toggle means to close the locker if it is open, and to open it if it's closed.
How many and which lockers are open in the end?
Thanks to ladaghini for submitting this challenge to /r/dailyprogrammer_ideas!
8
u/blisse Apr 05 '12
Can someone explain why the output is exactly all the squares? I sort of get it, but I would like a more in-depth answer.
eruonna just wrote a script for the result, which was cool, but didn't explain why it ended like that.
3
u/luxgladius 0 0 Apr 05 '12
Perl
Output
Open lockers: 31
1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961
4
u/eruonna Apr 05 '12
Haskell:
Output:
2
u/Ttl Apr 05 '12
Mathematica:
2
u/school_throwaway Apr 05 '12
python
2
u/HazzyPls 0 0 Apr 05 '12
C.
Tried to get a quick result without manually opening and closing each locker, or without simply squaring.
2
u/prophile Apr 06 '12
Python:
2
u/[deleted] Apr 05 '12
I'm open to suggestions on how to shrink this code
I get the same results as luxgladius
2
u/iluminumx Apr 05 '12
C++:
output: 1;4;9;16;25;36;49;64;81;100;121;144;169;196;225;256;289;324;361;400;441;484;529;576;625;676;729;784;841;900;961;
1
u/bob1000bob Apr 05 '12 edited Apr 05 '12
C++ code
compile
stdio
1
u/Iracus Apr 05 '12
Python
1
u/mazzer Apr 06 '12
Groovy:
1
u/notcleverenough Apr 06 '12
PHP
1
u/Zamarok Apr 06 '12 edited Apr 06 '12
What a great problem, and a surprising solution. Here's how I did it in Haskell:
1
u/[deleted] Apr 06 '12
Here's my complete program in C++. Answer is same as luxgladius.
I'm a beginner, so there are bound to be bad programming practices in the code. It would be great if seasoned programmers could point them out so I don't make same mistakes next time. :)
1
u/jarjarbinks77 0 0 Apr 06 '12 edited Apr 06 '12
My C++ attempt using vector pairs.
Compiled on Win7 x64 with MSVC++ 2010.
http://pastebin.com/rmhEGSsG
Here the simplified version with no arrays, vectors, or pairs.
http://pastebin.com/RwVxi5jU
Just a little shorter version.
http://pastebin.com/y1J45pDs
I think there still some stuff you could do to cut down on
processing time but its instant already on my computer.
1
u/namekuseijin Apr 07 '12
brute-force, but elegant scheme:
http://pastebin.com/WhpJd6iY
I've heard of it before, many years ago. I don't think I ever coded it though.
a surprising answer indeed...
1
u/[deleted] Apr 07 '12
Another C++ (using a plain ol' array)
edit:
1
u/1nvader Apr 07 '12
Same result in Python here:
31 lockers are open.
1
u/cooper6581 Apr 08 '12
Python (similar to other solutions already posted):
Output:
1
u/Reykd Apr 09 '12 edited Apr 09 '12
New to c++ here is my very long attempt:
Output:
PD: im certain that there is an easier way to hide all the code than to put four spaces before every single line of code... would anyone be so kind as to inform me of how?
1
u/bigronaldo May 29 '12
C#
1
u/devilsassassin Jun 22 '12
Java:
1
u/Should_I_say_this Jun 30 '12
output: 31 lockers open [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961]
0
u/[deleted] Apr 05 '12
[deleted] — view deleted comment