 Feature Articles
 CodeSOD

Error'd
 Most Recent Articles
 Zeroics
 Not Really an Error'd Error'd
 Twicely Done
 Fresh Bugs
 Absolutely Execrable
 Boy Howdy
 NaaN
 Bon Weekend
 Forums

Other Articles
 Random Article
 Other Series
 Alex's Soapbox
 Announcements
 Best of…
 Best of Email
 Best of the Sidebar
 Bring Your Own Code
 Coded Smorgasbord
 Mandatory Fun Day
 Off Topic
 Representative Line
 News Roundup
 Editor's Soapbox
 Software on the Rocks
 Souvenir Potpourri
 Sponsor Post
 Tales from the Interview
 The Daily WTF: Live
 Virtudyne
Admin
It depends on whether the locker number has an even or odd number of factors, even number = closed, odd number = open. Factors come in pairs unless the number is a square number where there is only a single factor multiplied by itself therefore if the number is a square then the locker is open otherwise closed. Simple really.
Admin
Python:
Admin
I got this question in an interview at an Internet startup. I solved in a few minutes (the previous poster stated the solution). I didn't get the job though, in part because they didn't like the way that I solved it :rolleyes. The whole thing was an Interview 2.0.
The "nerds" in these classes must have been real idiots to have never solved it faster than the jocks. I spent a few minutes on it, but even on paper you wouldn't get more than a couple iterations in, much less actually using physical lockers. I'm fairly intelligent, but even in high school I wasn't top of my class (~11 out of 250, though I was kinda lazy, that's why I like computers).
Admin
Lockers 1 was toggled once
Lockers 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, and 97 were toggled twice
Lockers 4, 9, 25, and 49 were toggled 3 times
Lockers 6, 8, 10, 14, 15, 21, 22, 26, 27, 33, 34, 35, 38, 39, 46, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, and 95 were toggled 4 times
Lockers 16, and 81 were toggled 5 times
Lockers 12, 18, 20, 28, 32, 44, 45, 50, 52, 63, 68, 75, 76, 92, 98, and 99 were toggled 6 times
Lockers 64 was toggled 7 times
Lockers 24, 30, 40, 42, 54, 56, 66, 70, 78, and 88 were toggled 8 times
Lockers 36, and 100 were toggled 9 times
Lockers 48, and 80 were toggled 10 times
Lockers 60, 72, 84, 90, and 96 were toggled 12 times
Admin
There are 5 lockers that were toggled 12 times.
Lockers 60, 72, 84, 90, and 96.
Admin
This first, it gets toggled only once, and remains open.
Admin
"The nerds never stood a chance, especially when it came to his "locker challenge."" lol, it took me less than 5 minutes to figure out that it was perfect squares, and judging by this article, those nerds are unworthy of the title for having so much trouble figuring it out.
Admin
Um...that's a WTF in itself. The only locker that is toggled only once: #1.
Admin
ruby makes me happy  so quick to write
Admin
given BruteForce is O(1) this problem appears to have amazingly less to do with a ftw.
Admin
Huh? No C++ Template Metaprogramming solution? Ok then:
Admin
Admin
I ran the simulation once, and saw the pattern, after you see it it's pretty easy. 0 is open 1 is closed.
def locker(n): result = "" c = 0 while (len(result) < n): result += ("1"*c) + "0" c += 2 return result print (locker(100))
Admin
oops, messed up formatting and realized I made a mistake
Admin
I glanced through the comments and couldn't find a SQL solution... So here it is.
 drop table #LockerChallenge create table #LockerChallenge ( LockerDoor int null, OpenClosed bit null, Toggled int null )
declare @i int
select @i = 0
while @i < 100 begin
select @i = @i + 1
insert into #LockerChallenge ( LockerDoor, OpenClosed, Toggled ) select @i,  LockerDoor 0,  OpenClosed 0  Toggled
end
declare @DoorNo int, @Increment int
select @DoorNo = 0, @Increment = 0
while @Increment < 100 begin
select @Increment = @Increment + 1
while @DoorNo <= 100 begin
end
select @DoorNo = 0
end
Admin
The very first locker gets opened the least...
Every other locker gets opened at least twice, once for the very first run, then for the nth run, where n is its locker number...
Admin
OMFG you guys are the biggest nerds ever, see you in the locker room, get ready for atomic wedgies!
Admin
Written in Myth
Admin
So... the number of times locker n is toggled is the number of divisors it has in the set of integers from 1 to 100. If locker n has an odd number of divisors (like that first locker does, having only one as a divisor) it ends up open. Otherwise, closed.
The teacher isn't really being very nice to the nerds here. If this had a really simple formula, wouldn't that mean we could easily do things like find primes and factor large numbers? You know, those things we base encryption on?
Admin
Ultracompact Befunge, made from:
This version contains 0% brute force.
Admin
Thats an easy one. There is only one locker that gets toggled only once. All others are toggled in the first round and in their round (at least).
Admin
Wait, let me reason this out a little more.
Every number will have one and itself among its divisors. Except for locker 1, that's two free toggles for everybody, which cancel one another out as far as the final open/shut state goes.
For locker n, if n is prime, that's it. No more divisors and the locker will end up shut.
If n is a perfect square, the square root of n will be another factor, so in that case we'll add another toggle and call it open.
Of course, the perfect square can easily still have other divisors we haven't considered yet. 16 for example has 2 and 8. 100 has a bunch. Somehow we still need to show that there will be an even number of these... argh, it's late. Maybe I'll wake up seeing the obviousness of it...
Admin
Too tired to read through all the comments but I saw an awful lot of 1+3+5+7+9+11... stuff in there being used as arguments against the i^2 method.
It's the same thing. using i as an iterator in F(i) F(i) returns the i'th locker to end in an open state. define F(1) = 1 (to make it explicit that i is not a zero based index) d is the absolute difference between F(i) and F(i1) F(i)F(i1) = 2i  1 as proven below. i^2  (i1)^2 = d : i^2  (i^2 2i + 1) = d :expand (i1)^2 i^2  i^2 + 2i 1 = d :extract terms from parenthesis 2i  1 = d :cancel i^2 terms
So yes guys. the difference between two adjacent squares is double the larger root minus 1.
Given that modern processors can do multiplication in just as many clock cycles as addition, the i+d method requires more registers to run and is probably going to be slightly slower than the i^2 method. Additionally, i^2 is easier to understand and maintain later.
Admin
Oh wait... it is simple. If n isn't a perfect square, you can pair off all its factors with the one number they'll need to multiply in order to get n.
If n is a perfect square, its square root can pair only with itself. Our locker game doesn't let you count a factor twice this way. On our "10" round, locker 100 gets touched once.
That's basically the whole thing right there. Why all the code solutions anyway? That's little better than the jocks here.
Admin
In Haskell, using prime factorization:
The result it quite predictable. The only times when the number of changes is odd is when each of the prime factorizations has an even power. So:
1 (with no factorizations) 2^2 > 4 3^2 > 9 2^4 > 16 5^2 > 25 2^2 * 3^2 > 36 7^2 > 49 2^6 > 64 9^2 > 81 2^2 * 5^2 > 100
Admin
Prime numbers  they'll only be toggled for 1 and themselves.
Admin
How does it work in c or c++? I'd assume it would do this: num := 100 c := 0
enter_while_loop enter_while_condition c := c + 1 compare: cc = 1 lte num = 100 end_compare false: exit_while true: continue enter_while_body sysout: cc = 1 loop_while
exit_program
Admin
Logo (KTurtle)
(no readed the previous comments, this is all my fault :D ) now for the code:
no costants?
$opened = 1 $closed = 0
learn toggle $X { $X1 = $closed if $X == $closed { $X1 = $opened } return $X1 }
learn test $howMany { if $howMany < 1 { print "no lockers, no party..." forward 20 exit } }
algorithm:
the locker is toggled if it is evenly divisible for a predecessor
test it for every predecessor
learn getOpenLockers $nLockers { test($nLockers)
1 is ever unlocked
print 1 forward 20
we start from 3 because 2 is ever locked
for $currentLocker = 3 to $nLockers { $thisLocker = $closed for $prevLocker = 2 to ($currentLocker 1) {
is evenly divisible, aka remainder is 0 ?
if ((round($currentLocker / $prevLocker)) * $prevLocker) == $currentLocker { $thisLocker = toggle ($thisLocker) } }
output
if $thisLocker == $opened { print $currentLocker forward 20 } } }
reset penup $howmanyLockers = ask "How many lockers?" print "Start" forward 20 getOpenLockers ($howmanyLockers) print "End" forward 20
now, for real :D
learn TR_getOpenLockers $nLockers { test($nLockers) for $i = 1 to round((sqrt($nLockers))0.5) { print $i * $i forward 20 } }
print "not really... :D " forward 20 TR_getOpenLockers ($howmanyLockers) print "this is the end... :)" forward 20
Admin
That sounds fine and dandy, but 36 doesn't stay closed... (see brute force jsimplementation in original Praxis posting).
Have you tried the brute force approach or did you just come up with a fancy "solution" to a problem and want to advertise it even though it's incorrect?
Admin
In case anyone is interested, this problem (only, with light switches) is covered at http://www.math.hmc.edu/funfacts along with many others.
Admin
The principle is very simple. Count the divisors of any number, including 1 and itself, and you get the number of times it's switched. If it's odd then it remains open.
The least switched number are of course the prime numbers, switched two times, and the 1, switched one time. The most switched are the numbers who have the greatest amount of divisors.
Admin
The first one, obviously. It's always toggled only one time.
Admin
The 'most' and 'least' questions are trivial. The least triggered locker is the first one, which only gets triggered once. The most triggered locker is the sixtieth, because it's a Highly Composite Number.
(Lockers are triggered whenever the number that comes up in the list is a divisor of the locker's number.)
Come to think about it, a locker is left open if it is toggled an odd number of times. In other words, a locker is left open only if its number has an odd number of divisors. But for every divisor of a number, there is another number, lockerNumber/divisor, which is also a divisor. Divisors come in pairs, in other words, which means numbers have an even number of divisors. Unless the number is square, at which point lockerNumber/divisor = divisor for one and only one divisor, the square root, which means it doesn't form a pair. So only squares have odd numbers of divisors. So only square numbered lockers are left open. That makes ten of them, since only the numbers one through ten have squares between one and one hundred inclusive.
10
...
Alright, I'll make a code solution, just give me a minute...
Admin
bool isOpen(int door) { return (floor(sqrt((double)door))==sqrt((double)door)) }
Admin
In bad Haskell...
Well, the 'clever' solution is
But the more normal solution is probably something along the lines of
Admin
To find the maximum number of divisors you need to find the largest highly composite number in the set  we'll call it n0 <= N. I don't know an easy way of generating highly composite numbers other than by filtering products of primorials. So
Step 1. Generate a list of primorials q_i <= N. This is easy  q_i is the product of the ith smallest primes (so q_0=1, q_1=2, q_2=23, q_3=235, q_4=2357, q_5=235711, etc.) We shall ignore q_0 subsequently, since it's not very helpful here. Step 2. For each number formed by the product of powers of primorials which is <= N (that part is a bit messy) compute the number of divisors. This is again easy  you effectively already have the factorisation. Select the largest number of divisors, and call that d. Step 3. Find all numbers <= N with d divisors: for every factorisation of derive a prime structure and use on small primes until you exceed N.
Worked example: N = 100. Step 1: Primorials <= 100 are 2, 6, 30. Step 2: Candidates are the primorials themselves (30 has 6 divisors), powers of two (64 has 7 divisors), powers of two multiplied by 6 (96 has 12 divisors), powers of two multiplied by 36 (72 has 12 divisors), and 230 = 60 (12 divisors). So d=12. Step 3: 12 factors as 112, 26, 223, 34; the corresponding prime structures are p0^11, p0^5 * p1, p0^2 * p1 * p2, p0^3 * p1^2. p0^11: No solutions <= 100 p0^5 * p1: 2^5 * 3 = 96 p0^2 * p1 * p2: 2^2 * 3 * 5 = 60, 2^2 * 3 * 7 = 84, 3^2 * 2 * 5 = 90 p0^3 * p1^2: 2^3 * 3^2 = 72
So the solution set is {96, 60, 84, 90, 72}.
Addendum (20090806 06:02): s/for every factorisation of derive/for every factorisation of d, derive/
Addendum (20090806 06:22): s/30 has 6 divisors/30 has 8 divisors/
Admin
the first, duh!
Admin
Well, I've realized that the test can stop at the half of the current locker number/position. So...
[code] for $prevLocker = 2 to (round($currentLocker/2)) { [code]
And, yes, I've learned the use of the "code" tag ^_^;
CYA
Admin
D'OH! >_<;;;
Admin
Though it is a bruteforce way of doing it  at least it is a solution in R.
This problem immediately remembered me of the Sieve of Eratosthenes.
Admin
Pick any sequence of powers to the N, write the difference of subsequent powers, then write the difference of subsequent differences, N times (after that all differences are zero). The differences at the Nth level will always be N!.
This is a fun fact that made me happy as a kid.
Admin
Since that was such fun, how about this:
"A magician deposits the same number of rabbits (at least one) at each of five houses. To get to the first house he crosses a magic river once, and to get to any house from another, he also crosses a magic river once. Each time he crosses a magic river, the number of rabbits he has doubles. He has no rabbits left when he leaves the fifth house. What is the minimum number of rabbits he could have at the start?"
Admin
That is probably the least efficient algorithm for finding perfect squares I could think of.
Here's a better (still shitty) one that runs in O(logN) ish time.
void find_open_lockers(char *lockers[], int num_lockers) { int i = 1;
do { lockers[(ii)1] = 1; i+=1; if ((ii) >num_lockers) return; } while (1)
}
Admin
Looked at the comments too soon.
I was working through the problem (without the timer as I'm a coward ;)) and had gotten to the fact that it comes down to the number of factors (or rather the oddness/evenness of the number of factors). I hadn't made the step to the fact that only square numbers have an even number of factors though, so failed at the challenge :(
That said, here's a quick python solution anyway ;)
Addendum (20090806 07:33): Wow... looks like that's the shortest python solution so far.
Has everyone else posting python forgotten about list comprehensions, the builtin power operator and the fact that int() performs a floor? As shown, makes it a oneliner :)
Addendum (20090806 07:35): Also, I meant in my original post that I hadn't noticed that only squares had odd numbers of factors, not even.
Admin
Proof that without work, there's no Collatz.*
Actually this appears not to be the Collatz theorem (or really the Collatz conjecture) anyway  that's the one about the sequence of numbers generated by the rule "if N is even, halve it, else triple it and add 1". The Collatz conjecture is that if N starts as a positive integer, it always reaches the value 1.
http://en.wikipedia.org/wiki/Collatz_conjecture
Admin
C# 3.5:
This assumes that the ForEach extensionfunction for IEnumerable is defined, which in my projects it always is. But the Mofties forgot it somehow, so with plain .NET 3.5 you have to define it yourself or work around it like
Captcha: "validus", so I guess I have to provide the proof: Every door is open that gets toggled an odd number of times. The number of times each door is toggled is equal to its number of dividers (follows directly from the used algorithm). If X is the number of doors, then for each divider N of X that is smaller than sqrt(X) there must be a number M greater than sqrt(X) such that NM = X (else N wouldn't be a divider of X and if M <= sqrt(x) then NM < sqrt(x)*sqrt(x) = X). So M is a divider of X, too, which makes the number of dividers even, except when sqrt(X) is itself an divider of X, which is true only for squarenumbers. So the open doors correspond to all square numbers (including 1) smaller or equal than X.
BTW: The Jocks still won in my case :/
Admin
If my understanding of the problem is correct then the answer is 1, and he leaves 2 rabbits in each house. One way to accomplish this is to visit each house except the fifth twice.
Admin
1 will be toggled least :)
Admin
I've worked with guys like you: math background (right?), thinks of himself as much better than all those lowly computer scientists around him, hired as a programmer because there is zero money in math, but refuses to do menial work like actually programming because there's just no challenge in it for a genius like himself.
I've also seen the disorganized piles of shit that such people call code, after they had finally left. And then I'm grateful I don't work with them right now.
Anyway, I feel for you: living between all those stupid people, and chances are you will never solve a new problem in your life. Happiness must be hard to reach under those conditions...
Admin