Results 1 to 10 of 10

Thread: Brain Teaser For Fun

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Chicago
    Posts
    271

    Brain Teaser For Fun

    In a recent issue of Games Magazine the following question was posed:

    There are 100 closed lockers in a school. Standing in front of each locker is a student. A bell will ring 100 times. At the first ring, all students will toggle their locker (from closed to opened). At the second ring, every other student will toggle his or her locker. At the third ring, every third student will toggle his or her locker, and so on, until the 100th ring where only the 100th student will toggle his/her locker. After the 100th ring, how many lockers will be open?

    Now I wrote a program to determine the answer and it is quite intriguing. I'm wondering if anyone knows why the answer (which you'll have to figure out) is what it is, I needed the program but with time, I think I could have figured out the simple solution. I'll post the program in a couple of days unless someone else beats me to it.
    Sometimes what you're looking for is exactly where you left it.

  2. #2
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking Hehe...sounds familiar

    Yeah, the answer is (look away if you don't want to know)

    10 lockers will be open.
    In particular, the open lockers will be #1, 4, 9, 16, 25, 36, 49, 64, 81 and 100.

    Reasoning:
    consider a locker #X. When is it opened/closed?? i.e. when does its state change?? Obviously, the locker is opened on the 1st ring of the bell. Next, on the 2nd ring, if the locker number is even (divisible by two) it will change state. Similarly,on the 3rd ring, if X is divisible by 3 (written as "3|X") then the state of locker X will change.

    It is easy from this to see that at the nth ring, the state of X will change if n|X
    => X changes state at every ring (#k) which is a factor of it. (k|X)

    i.e. when does #12 change state??
    at ring #1, 2, 3, 4, 6 and 12 = 6 times
    As it changes state an even number of times, it is closed (easy to verify)

    In other words, if an integer X has an Even number of factors, it is closed at the end, if it has an odd number, locker X is open.
    So, what numbers have an odd number of factors?? ONLY SQUARES. This can be explained formally, but an easier proof is to say that every factor d of X can be paired up with another factor, X/d, SO LONG AS d <> x/d. (Otherwise, you are counting the same number twice)
    => Every factor can be paired up => A number always has an even numbner of factors, UNLESS there is a number d such that d=X/d. This is easily changed into d2=X, or X is a square number.

    Summary:
    Q) How many lockers are open?
    A) 10, they are the lockers whose number is one of the squares 1 to 100, numbers which have an odd number of factors, and so are open at the end.
    sql_lall

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Chicago
    Posts
    271

    Thumbs up

    Correct answer and excellent explanation
    Sometimes what you're looking for is exactly where you left it.

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    I made the code for EXCEL-VBA

    VB Code:
    1. Option Explicit
    2. Option Base 1
    3.  
    4. Public Sub Locker()
    5. Dim i As Integer
    6. Dim j As Integer
    7. Dim Locker(100) As String
    8. For i = 1 To 100
    9.     Locker(i) = "CLOSED"
    10. Next i
    11. For i = 1 To 100
    12.     For j = 1 To 100
    13.         If j / i = Int(j / i) Then
    14.             If Locker(j) = "CLOSED" Then
    15.                 Locker(j) = "OPEN"
    16.             Else
    17.                 Locker(j) = "CLOSED"
    18.             End If
    19.         End If
    20.         ActiveSheet.Cells(i, j).Value = CStr(Locker(j))
    21.     Next j
    22. Next i
    23. ActiveSheet.Cells(101, 1).Value = "Open Lockers:"
    24. For i = 1 To 100
    25.     If ActiveSheet.Cells(100, i).Value = "OPEN" Then
    26.         ActiveSheet.Cells(101, 2).Value = ActiveSheet.Cells(101, 2).Value & "," & CStr(i)
    27.     End If
    28. Next i
    29. End Sub
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Chicago
    Posts
    271
    Yes, that code works. And here is some code in VB, you need a form with a wide listbox, a commandbutton and a textbox. Just enter any number in the text box for the number of lockers you want to check.
    VB Code:
    1. Option Explicit
    2.  
    3. Dim aBits() As Boolean
    4. Dim iNum As Integer
    5. Dim sNum As String
    6.  
    7. Private Sub Command1_Click()
    8.   ReDim aBits(txtMax - 1)
    9.   List1.Clear
    10.   GoFigure
    11. End Sub
    12.  
    13. Private Sub GoFigure()
    14. Dim i, j As Integer
    15.   j = 1
    16. LoopAgain:
    17.   'toggle appropriate bits base on j value
    18.   For i = 0 + (j - 1) To txtMax - 1 Step j
    19.     aBits(i) = Not aBits(i)
    20.   Next
    21.   'record bits for display in list
    22.   For i = 0 To txtMax - 1
    23.     If aBits(i) = True Then
    24.       iNum = 1 'open
    25.     Else
    26.       iNum = 0 'closed
    27.     End If
    28.     sNum = sNum & iNum
    29.   Next
    30.   List1.AddItem sNum
    31.   sNum = ""
    32.   'when max is reached reset bits for next calculation and exit sub
    33.   If j = txtMax Then
    34.     For i = 0 To txtMax - 1
    35.       aBits(i) = False
    36.     Next
    37.     Exit Sub
    38.   End If
    39.   'continue calculating
    40.   j = j + 1
    41.   GoTo LoopAgain
    42. End Sub
    Sometimes what you're looking for is exactly where you left it.

  6. #6
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571
    Just noticed something by opus:
    VB Code:
    1. If Locker(j) = "CLOSED" Then
    2.    Locker(j) = "OPEN"
    3. Else
    4.    Locker(j) = "CLOSED"
    5. End If

    You don't need the else part, cos it will remain "CLOSED" anyway
    Also JohnVB6, there are some faster ways to do your stuff too, like using IIF, looping j = 1 to txtMax, and including sNum = sNum & iNum in the IIF statement.

    Anyway, Private Sub 'GoFigure()' hehehe
    sql_lall

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Location
    Chicago
    Posts
    271
    Thanks for the iinfo, I never have used the IIF statement.
    Sometimes what you're looking for is exactly where you left it.

  8. #8
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Thanks for hint sql_lall, however

    if Locker(i) is "OPEN" I want it to change to "CLOSE", so I do need the ELSE statement (I think!)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  9. #9
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking Sorry

    So sorry, i get what it means now.
    You do need it for that way, but you can also use IIF. I actually had never heard of it for ages until a few months ago. One of those really helpful things that never appears in books for some odd reason. K
    sql_lall

  10. #10
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551

    Re: Sorry

    Originally posted by sql_lall
    One of those really helpful things that never appears in books for some odd reason. K
    Maybe because it is terribly slow? I once thought it to be a handy function (compact code), but for some reason it is very slow. An if-then-else block is much faster.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width