| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Next » |
|
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. |
|
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); } } |
|
#!perl
sub lockers { return map $_**2, 1..sqrt shift } |
|
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! |
Re: Nerds, Jocks, and Lockers
2009-08-05 09:14
•
by
Obvious
(unregistered)
|
|
Umm squares of 1 to 10?
Captcha "eros" .....I love you too! |
Re: Nerds, Jocks, and Lockers
2009-08-05 09:16
•
by
Mark
(unregistered)
|
|
Or to put it more simply- The open lockers are i^2 where i runs from 1-10.
|
|
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? |
Re: Nerds, Jocks, and Lockers
2009-08-05 09:17
•
by
Savvy Business User
(unregistered)
|
|
Paste this formula in Excel and use copy-down
=ROW(A1)^2 |
Re: Nerds, Jocks, and Lockers
2009-08-05 09:19
•
by
Addison
(unregistered)
|
Somehow I think that makes it less fun. |
|
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 |
That's good because I don't find solving solved problems to be fun in the first place. |
Re: Nerds, Jocks, and Lockers
2009-08-05 09:28
•
by
Florian Junker
(unregistered)
|
|
Haskell:
lockers n = takeWhile (<= n) [x*x|x<-[1..]] |
|
Looks like the most-toggled locker is the 96th one, with 12 toggles.
|
|
DOS, because I'm too lazy to SSH into my linux machine:
|
|
for me, I saw the pattern +3, +5 , +7, +9, +11, +13, +15, +17 and +19
not i^2 |
Re: Nerds, Jocks, and Lockers
2009-08-05 09:33
•
by
Daniel
(unregistered)
|
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 |
|
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?
|
Re: Nerds, Jocks, and Lockers
2009-08-05 09:37
•
by
halber_mensch
|
|
Not exactly awesome:
|
So... Not having fun in your job as a programmer ? |
Thanks for pointing out the rythm! Classic ASP:
|
Not particularly. |
|
Thinking just shortly (1-2 min) about it, I'd say that exactly the square numbers stay open.
|
Re: Nerds, Jocks, and Lockers
2009-08-05 09:41
•
by
Frunobulax2099
(unregistered)
|
|
input: totalLockerNum
max = floor(sqrt(totalLockerNum)) for i=1; i<=max; i++ { result = i**2; add result to array } return array |
Re: Nerds, Jocks, and Lockers
2009-08-05 09:41
•
by
Kris
(unregistered)
|
Yes, that would be the pattern for perfect squares. |
|
.NET Solution:
|
|
i just watched the example and i feel like a jock
stupid nerds with their fancy "square" numbers, nobody needs em! |
Re: Nerds, Jocks, and Lockers
2009-08-05 09:46
•
by
Anon
(unregistered)
|
How sad :( |
|
what with the brilliant paula being brillant?
|
|
Answer:
perfect squares. using formulae:
Brute force:
Solved this ages ago for rosetta code: (http://rosettacode.org/wiki/100_doors#Common_Lisp) |
|
Python:
import math; def getOpenLockers(i): return [x**2 for x in range(1, 1+int(math.sqrt(i)))] |
|
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 |
Re: Nerds, Jocks, and Lockers
2009-08-05 09:53
•
by
Gabriel
(unregistered)
|
|
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. |
Re: Nerds, Jocks, and Lockers
2009-08-05 09:57
•
by
Jim
(unregistered)
|
|
Yup, only one small change....
|
|
Might as well bandwagon on the languages thing:
You can download it here I'm getting eaten by the spam detector, uh oh. |
|
Ah, I think I see the WTF here...
|
Re: Nerds, Jocks, and Lockers
2009-08-05 09:58
•
by
Steve the Cynic
(unregistered)
|
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. |
|
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() ) |
What about numbers like 1*2*3*4 = 24 or 2*3*4*7 = 168 then? I don't see where it says n should be less than 100. |
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. |
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. |
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. |
|
Wrote this quick in C#
Console.Write("How many lockers? "); int lockers = Convert.ToInt32(Console.ReadLine()); int openlockers = 0; int interval = 1; Console.Write("Open Lockers: "); while (openlockers + interval < lockers) { openlockers += interval; Console.Write(openlockers + " "); interval += 2; } |
|
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 |
|
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! |
Re: Nerds, Jocks, and Lockers
2009-08-05 10:12
•
by
Medinoc
(unregistered)
|
24 can be divided by 1, 2, 3, 4, 6, 8, 12 and 24. That's 8 factors, or 6 if we exclude the extrema -> even 25 can be divided by 1, 5 and 25. That's 3 factors -> odd. |
Re: Nerds, Jocks, and Lockers
2009-08-05 10:12
•
by
steenbergh
|
|
And a slightly smaller version in JS
BTW, I'm getting an awful lot of errors when posting lately... |
#!/usr/bin/python |
Re: Nerds, Jocks, and Lockers
2009-08-05 10:12
•
by
Steve the Cynic
(unregistered)
|
|
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 |
|
simply - squares of prime numbers (or perfect squares)
I'm amazed with amount of i^2 solutions. Think a bit longer before posting. |
| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Next » |