i don't have a method of solving this problem, but i can clearly see the pattern when it's done - closed lockers in runs of 2,4,6,8,10,12,14,16,18 with a single open locker between them

The reasoning is simple: non-perfect squares have an even number of factors. The jocks toggle a number for every factor it has. Therefore every door should be toggled an even number of times and should therefore end closed (the starting state).

However, perfect squares have an odd number of unique factors, because one of the factors is repeated. Because of this, perfect squares are toggled for their square root which has no pair to reverse the toggle, so the door ends up open (the opposite state it started in).

The reasoning is simple: non-perfect squares have an even number of factors. The jocks toggle a number for every factor it has. Therefore every door should be toggled an even number of times and should therefore end closed (the starting state).

However, perfect squares have an odd number of unique factors, because one of the factors is repeated. Because of this, perfect squares are toggled for their square root which has no pair to reverse the toggle, so the door ends up open (the opposite state it started in).

The reasoning is simple: non-perfect squares have an even number of factors. The jocks toggle a number for every factor it has. Therefore every door should be toggled an even number of times and should therefore end closed (the starting state).

However, perfect squares have an odd number of unique factors, because one of the factors is repeated. Because of this, perfect squares are toggled for their square root which has no pair to reverse the toggle, so the door ends up open (the opposite state it started in).

Make sense?

Somehow I think that makes it less fun.

That's good because I don't find solving solved problems to be fun in the first place.

i don't have a method of solving this problem, but i can clearly see the pattern when it's done - closed lockers in runs of 2,4,6,8,10,12,14,16,18 with a single open locker between them

how delightful!

I also noticed this pattern and it is trivial to code:

void lockers(int n)
{
int i, j, k;
for (i = 1, k = 2; i <= n;)="" {="" printf="" ("o")="" i++;="" for="" (j="0;" (i=""></=><= n)="" &&="" (j=""></=>< k);="" i++,="" k++)="" printf="" ("x");="" k="k+k;" }="" }="">

From the explanation about the squares, we can deduce why this works:

i don't have a method of solving this problem, but i can clearly see the pattern when it's done - closed lockers in runs of 2,4,6,8,10,12,14,16,18 with a single open locker between them

how delightful!

Thanks for pointing out the rythm!

Classic ASP:

<%
function doLockers(numLocks)
dim Lockers()
redim Lockers(numLocks-1)
dim inc : inc = 1
dim lastOpened : lastOpened = 0
dim nextToOpen : nextToOpen = 0
' closing all lockers
for i = 0 to numLocks-1
Lockers(i) = 0
next
' Jocking through the corridor...
do while nextToOpen < numLocks
Lockers(nextToOpen) = 1 ' open locker
lastOpened = nextToOpen
inc = inc + 2 ' skip ahead
nextToOpen = lastOpened + inc
loop
' printing result
dim ret : ret = "Open lockers: "
for i = 0 to numLocks-1
if Lockers(i) = 1 then
ret = ret & " " & cstr(i+1)
end if
next
doLockers = ret
end function
response.write ""
response.write "" & doLockers(100)
%>

SQL - though the use of numbers table may qualify as brute force...

create FUNCTION [dbo].[getOpenLockersList] ()

RETURNS @aReturnTable TABLE
( locker INT,
isOpen BIT,
myCount INT)
AS BEGIN

INSERT INTO @aReturnTable (locker, mycount)
SELECT locker, SUM(CASE WHEN locker%number = 0 THEN 1 ELSE 0 END)
FROM (SELECT number locker FROM numbers WHERE number <= 100) lcker
CROSS join
(SELECT number FROM numbers WHERE number <= 100) nbr
GROUP BY locker
return

END
GO
SELECT locker,
CASE WHEN mycount%2 = 1 THEN 'OPEN' ELSE 'closed' END lockerState ,
myCount
FROM dbo.getOpenLockersList()

SELECT SUM(mycount%2) FROM dbo.getOpenLockersList()
SELECT locker FROM dbo.getOpenLockersList()
WHERE mycount =
(SELECT MAX(mycount) FROM dbo.getOpenLockersList() )

The reasoning is simple: non-perfect squares have an even number of factors. The jocks toggle a number for every factor it has. Therefore every door should be toggled an even number of times and should therefore end closed (the starting state).

However, perfect squares have an odd number of unique factors, because one of the factors is repeated. Because of this, perfect squares are toggled for their square root which has no pair to reverse the toggle, so the door ends up open (the opposite state it started in).

Make sense?

What about numbers like 1234 = 24 or 2347 = 168 then?

I don't see where it says n should be less than 100.

The reasoning is simple: non-perfect squares have an even number of factors. The jocks toggle a number for every factor it has. Therefore every door should be toggled an even number of times and should therefore end closed (the starting state).

However, perfect squares have an odd number of unique factors, because one of the factors is repeated. Because of this, perfect squares are toggled for their square root which has no pair to reverse the toggle, so the door ends up open (the opposite state it started in).

Make sense?

What about numbers like 1234 = 24 or 2347 = 168 then?

I said factors. 24's factors are {1, 2, 3, 4, 6, 8, 12, 24}. There is an even number of them.

And the limit of 100 is irrelevant. That just gives us the maximum perfect square we care about.

The reasoning is simple: non-perfect squares have an even number of factors. The jocks toggle a number for every factor it has. Therefore every door should be toggled an even number of times and should therefore end closed (the starting state).

However, perfect squares have an odd number of unique factors, because one of the factors is repeated. Because of this, perfect squares are toggled for their square root which has no pair to reverse the toggle, so the door ends up open (the opposite state it started in).

Make sense?

What about numbers like 1234 = 24 or 2347 = 168 then?

I said factors. 24's factors are {1, 2, 3, 4, 6, 8, 12, 24}. There is an even number of them.

Yeah, I was going to delete after posting, but it was to late.
I was thinking of a slight different problem, I guess; where you don't start toggling at the i-th locker for the i-th iteration.

The reasoning is simple: non-perfect squares have an even number of factors. The jocks toggle a number for every factor it has. Therefore every door should be toggled an even number of times and should therefore end closed (the starting state).

However, perfect squares have an odd number of unique factors, because one of the factors is repeated. Because of this, perfect squares are toggled for their square root which has no pair to reverse the toggle, so the door ends up open (the opposite state it started in).

Make sense?

What about numbers like 1234 = 24 or 2347 = 168 then?

I said factors. 24's factors are {1, 2, 3, 4, 6, 8, 12, 24}. There is an even number of them.

Yeah, I was going to delete after posting, but it was too late.
I was thinking of a slight different problem, I guess; where you don't start toggling at the i-th locker for the i-th iteration.

Addendum (2009-08-05 10:12):
Addendum: never mind. I guess I'm just not thinking straight about this.

The locker toggled the most is the locker whose prime factors can create the greatest number of combinations. (locker ... combinations ... get it?).

At first, I think 64 might be a good choice, but (2,2,2,2,2,2) creates only 6 combinations. If I replace a 2 with a 3, I describe locker 96, and I can now create 11 combinations. Applying my knowledge of cribbage, I see that I can replace three 2s with two 3s to describe locker 72 (2,2,2,3,3), which also creates 11 combinations.

I would bet that Mr. Zargas was the coach of the football team. This challenge is EXTREMELY biased towards the jocks. I respectfully submit that in order to figure out the function, you HAVE to iterate (brute force) at SOME point in time.

Admin

Programming Praxis has officially gotten its own category. Next up: perhaps adding a field for the programming language, so we can build an index.

As for last week's, there were some great solutions... some of the more interesting ones involved XSLT, ZX Spectrum BASIC version, Piet, and as a Minsky machine.

Admin

public static void main(String[] args) { int num = 100; int i=1; int k=1; while(k<num){ k=i*i; i++; System.out.println(k); }

Admin

#!perl

sub lockers { return map $_**2, 1..sqrt shift }

Admin

i don't have a method of solving this problem, but i can clearly see the pattern when it's done - closed lockers in runs of 2,4,6,8,10,12,14,16,18 with a single open locker between them

how delightful!

Admin

Umm squares of 1 to 10?

Captcha "eros" .....I love you too!

Admin

Or to put it more simply- The open lockers are i^2 where i runs from 1-10.

Admin

The open lockers are perfect squares.

The reasoning is simple: non-perfect squares have an even number of factors. The jocks toggle a number for every factor it has. Therefore every door should be toggled an even number of times and should therefore end closed (the starting state).

However, perfect squares have an odd number of unique factors, because one of the factors is repeated. Because of this, perfect squares are toggled for their square root which has no pair to reverse the toggle, so the door ends up open (the opposite state it started in).

Make sense?

Admin

Paste this formula in Excel and use copy-down

=ROW(A1)^2

Admin

Somehow I think that makes it less fun.

Admin

vbs file, work in nearly any windows

a = inputbox("locker?")

for i = 1 to a l = l+1 b = b & i & " " i = i + (2*l) next

msgbox b

Admin

Admin

Haskell:

Admin

Looks like the most-toggled locker is the 96th one, with 12 toggles.

Admin

DOS, because I'm too lazy to SSH into my linux machine:

Admin

for me, I saw the pattern +3, +5 , +7, +9, +11, +13, +15, +17 and +19

not i^2

Admin

I also noticed this pattern and it is trivial to code:

From the explanation about the squares, we can deduce why this works:

(n+1)^2 = n^2 + 2n + 1

Admin

The header of the article shows up as white on white in chrome, maybe this category hasn't had a header color assigned to it yet?

Admin

Not exactly awesome:

Admin

So... Not having fun in your job as a programmer ?

Admin

Thanks for pointing out the rythm!

Classic ASP:

Admin

Admin

Thinking just shortly (1-2 min) about it, I'd say that exactly the square numbers stay open.

Admin

input: totalLockerNum

max = floor(sqrt(totalLockerNum))

for i=1; i<=max; i++ { result = i**2; add result to array } return array

Admin

Yes, that would be the pattern for perfect squares.

Admin

.NET Solution:

Admin

i just watched the example and i feel like a jock

stupid nerds with their fancy "square" numbers, nobody needs em!

Admin

How sad :(

Admin

what with the brilliant paula being brillant?

Admin

Answer:perfect squares.

using formulae:Brute force:Solved this ages ago for rosetta code: (http://rosettacode.org/wiki/100_doors#Common_Lisp)

Admin

Python:

Admin

Using this excuse to practice some Groovy:

public List getLockers(int numLockers) { (1..numLockers).collect{ it*it } }

Any groovy programmers that want to show me better ways to do this, please point them out. :D

Admin

Pretty simple :) after you watch the simulation... getOpenLockers(n){open = 1;count = 3; while(open<=n) { print open; open += count;count += 2;} } I'll leave the mathematical solution for somebody else.

Admin

Yup, only one small change....

Admin

Might as well bandwagon on the languages thing:

You can download it here

I'm getting eaten by the spam detector, uh oh.

Admin

Ah, I think I see the WTF here...

[image]Admin

I AM NOT A GROOVY PROGRAMMER.

It looks like it will return the squares from 1 to numLockers**2, which might not be what you want.

Admin

SQL - though the use of numbers table may qualify as brute force...

create FUNCTION [dbo].[getOpenLockersList] ()

RETURNS @aReturnTable TABLE ( locker INT, isOpen BIT, myCount INT) AS BEGIN

END GO SELECT locker, CASE WHEN mycount%2 = 1 THEN 'OPEN' ELSE 'closed' END lockerState , myCount FROM dbo.getOpenLockersList()

SELECT SUM(mycount%2) FROM dbo.getOpenLockersList() SELECT locker FROM dbo.getOpenLockersList() WHERE mycount = (SELECT MAX(mycount) FROM dbo.getOpenLockersList() )

Admin

234 = 24 or 2347 = 168 then?I don't see where it says n should be less than 100.

Admin

And the limit of 100 is irrelevant. That just gives us the maximum perfect square we care about.

Admin

Admin

Addendum (2009-08-05 10:12): Addendum: never mind. I guess I'm just not thinking straight about this.Admin

The locker toggled the most is the locker whose prime factors can create the greatest number of combinations. (locker ... combinations ... get it?).

At first, I think 64 might be a good choice, but (2,2,2,2,2,2) creates only 6 combinations. If I replace a 2 with a 3, I describe locker 96, and I can now create 11 combinations. Applying my knowledge of cribbage, I see that I can replace three 2s with two 3s to describe locker 72 (2,2,2,3,3), which also creates 11 combinations.

Admin

Wrote this quick in C#

Admin

Even worse is when the problem was already solved by Tom and Ray on Car Talk:

http://www.cartalk.com/content/puzzler/transcripts/200546/answer.html

Admin

I would bet that Mr. Zargas was the coach of the football team. This challenge is EXTREMELY biased towards the jocks. I respectfully submit that in order to figure out the function, you HAVE to iterate (brute force) at SOME point in time.

SHENANIGANS!

Admin

25 can be divided by 1, 5 and 25. That's 3 factors -> odd.

Admin

And a slightly smaller version in JS

BTW, I'm getting an awful lot of errors when posting lately...

Admin

Admin

Re: "96 has the most"...

Other numbers not above 100 with 12 distinct factors:

60: 1 2 3 4 5 6 10 12 15 20 30 60 72: 1 2 3 4 6 8 9 12 18 24 36 72 84: 1 2 3 4 6 7 12 14 21 28 42 84

Admin

simply - squares of prime numbers (or perfect squares)

I'm amazed with amount of i^2 solutions. Think a bit longer before posting.