• Sam Wilson (unregistered)

    I'm not really a math geek (been years) but I think that a friend and I came up with a decent formula, but not sure how to express it :) after that, we realized that all squares are open lockers, so our "solution" became more a proof of why squares are open lockers

    We initial figured that a locker "toggles" when a factor of the number is encountered. Then figured that an odd number of factors would leave the locker open. The formula is something like the sum of all instance from 1 to x (where in this case x is 100) where the number of factors of x is odd. So starting off: 1 has 1 (1 - odd) 2 has 2 (1 2 - even) 3 has 2 (1 3 - even) 4 has 3 (1 2 4 - odd) 5 has 2 (1 5 - even) ... and so on. Further down the locker line... 48 has 10 (1 2 3 4 6 8 12 16 24 48 - even) 49 has 3 (1 7 49 - odd) 50 has 6 (1 2 5 10 25 50 - even) 51 has 4 (1 3 17 51 - even)

    Looking at this, we realize that squares are odd because we only count it's square factor once, when it multiplies itself.

    As far as least toggled locker, locker #1 only once :) next in line, all prime number are toggled twice (1 and itself), naturally all being closed lockers

    Sam

  • Sam Wilson (unregistered) in reply to Sam Wilson

    I just saw the 8 pages of comments, and realized that it's been solved already :( oh well, my last post stands, I figured it out while drinking beers :)

  • Dave (unregistered)
    Comment held for moderation.
  • Dave (unregistered) in reply to Dave

    comparable cpu effort for 10^4 to 10^9 lockers (at least as reported)

  • Ed Gorcenski (unregistered)

    MATLAB code:

    n = 100;

    L = zeros(1,n); mask = 1:n; toggle = zeros(n);

    for i=1:n toggle(i,:) = mod(mask,i)==0; L = L + toggle(i,:); L(find(L>1)) = 0; end L t = sum(toggle); least = find(t==min(t)) most = find(t==max(t))

  • Jürgen (unregistered) in reply to Maurits

    def least_toggled(n): return 1

  • (cs) in reply to Ed Gorcenski
    Ed Gorcenski:
    MATLAB code:

    Make that toggle = zeros(1, n); or even just toggle = L;

    (zeros(n) is an n x n matrix)

  • David (unregistered)

    It's easy. 11 = 1 22 = 4 33 = 9 44 = 16 55 = 25 66 = 36 77 = 49 88 = 64 99 = 81 1010=100

    done.

  • David (unregistered) in reply to Ed Gorcenski

    matlab? pfft. ;)

    You're still brutin' it.

  • quintopia (unregistered)

    Without looking at previous comments:

    Open: Even powers of primes (1,4,9,16,25,49,64,81) Closed: Everything else.

    Most toggled: Whatever has the most prime factors (too lazy to figure it out, so I'm going to guess 60)

    Least toggled: 1 (duh) and in second place, all primes.

  • quintopia (unregistered)

    Yeah I got it wrong. All squares have an odd number of factors, not just even powers of primes. Oh well.

  • Minko (unregistered)

    Simple: each locker with an index that is a prime number will be closed. All of these will be toggled just twice. For the rest they will be either open or closed depending on the number of unique dividers. For example 12 has 5 unique dividers 1, 2, 3, 4 and 6. It will be open.

  • Minko (unregistered) in reply to Minko

    ooops forgot 12 is also a divider. It will actually be closed :)

  • Tama (unregistered) in reply to Mathamagician
    Mathamagician:
    Tama:
    Technically, this is incorrect; more than one factor may be repeated. To prove that a number has an odd number of factors if and only if it is a perfect prime:

    Let x be an integer, x = x1^a1 * x2^a2 * ... * xn^an. For a given factor, there are (a1 + 1) ways to choose at what power x1 will be, (a2 + 1) ways to choose at what power x2 will be, and so on. The number of distinct factors for x is thus p = (a1 + 1) * (a2 + 1) * ... * (an + 1). Now, p is odd if and only if all of (a1 + 1), (a2 + 1), ..., (an + 1) are odd, or equivalently, if and only if a1, a2, ..., an are even. That last condition is equivalent to saying that x is a perfect prime.

    uhm...no

    36 has factors 1, 2, 3, 4, 6, 9, 18, 36 (7 of them) does this mean 36 is prime?? It has an even number of prime factors (2 and 3) which might be what you meant....

    I meant "perfect square" of course, not "perfect prime".

  • Dan (unregistered) in reply to Maurits

    Locker 1 and all primes are toggled least (twice).

  • Aaron M. (unregistered)
    Comment held for moderation.
  • (cs) in reply to Biff Tannen
    Biff Tannen:
    OMFG you guys are the biggest nerds ever, see you in the locker room, get ready for atomic wedgies!
    See you in your car; get ready for a load of manure!
  • Anonerd (unregistered) in reply to Nigl
    Nigl:
    Write a program that alphabetically sorts the numbers from one to a billion (we will exclude spaces and 'and' so 4567 would be fourthousandfivehundredsixtyseven). Counting characters in this sorted list, we see that the twenty-eighth letter is the end of eighteenmillion (only eight and eighteen precede it)). the 51 billionth letter also happens to be the last letter of a spelt out number - What is the number, and what is the sum of the integers up to that point?

    Granted this particular question may be too complex for this sort of forum, but it serves a good example because it requires very little knowledge other than data structures and algorithms...

    I'm Kent Brockman

    For extra points, do not assume English; support at least all languages spoken in the EU.

    Seriously, Mrs MeChokemchild suggested a harder problem:

    It doesn't say "after performing toggles for i=1..n on n lockers", it doesn't even say "after toggling every mth locker for m=1..n". It says "after toggling n lockers" where one toggling is defined as "changing the state of one door".

    Admittedly the simulator takes the number of lockers as input, not the number of togglings, but then the simulation was 'To Get Started' only.

    Captcha: capio - I got it! (?)

  • AllanW (unregistered) in reply to Maurits

    Uh... Maurits's "follow-up question" is much too easy: No mater how many lockers you have, locker #1 is only ever touched once.

  • Jeremy Pyne (unregistered)

    Meh, generic C#...

    using System; using System.Collections.Generic; using System.Linq; using System.Text;

    namespace ConsoleApplication1 { class Program {

        static bool[] doLockers(int size, int iteration)
        {
            bool[] data;
    
            if (iteration == 1)
                data = new bool[size];
            else
                data = doLockers(size, iteration - 1);
    
            for (int count = iteration - 1; count < size; count = count + iteration)
                data[count] = !data[count];
    
            return data;
        }
    
        static void Main(string[] args)
        {
            Console.Write("Enter a number of lockers to toggle: ");
            int count = Convert.ToInt32(Console.ReadLine());
    
            Console.WriteLine("Results: ");
            foreach (bool item in doLockers(count, count))
                Console.Write(item ? "1" : "0");
            Console.WriteLine();
    
            Console.WriteLine("Press any key to exit");
            Console.ReadLine();
        }
    }
    

    }

  • Ethan (unregistered) in reply to Maurits

    locker #1 is toggled only once. The prime number ones are toggled exactly twice, by one and the prime they represent.

  • rjk (unregistered)

    Hmmmm... wonder if anyone has tried a Texas Instruments calculator version. Would like to see it run on my crappy TI-83 Plus!

  • Barett McGavock (unregistered) in reply to Maurits

    Obviously, locker 1 is toggled only once. Lockers with prime numbers are toggled twice: once on the first iteration, and once for itself. For others, find the factors of the locker number. The number of factors equals the number of times it's toggled.

  • CS (unregistered) in reply to Maurits

    The 1 followed by the primes are toggled the least. The number of toggles is based upon the number of factors.

    1: 1 toggle Primes: 2 toggles Perfect Squares: Odd number of toggles, hence open Other: Even number of toggles, hence closed

  • Cranberry (unregistered) in reply to Maurits

    Prime numbered ones. They're toggled twice, one at the first step, and once at their own.

    As a general rule, each locker is toggled a number of times equal to the number of possible unique groupings of its prime factors that are <= the number of lockers. So for example, take the 20th locker, with factors 2,2,5. It gets toggled:

    On the 1st step. On the 2nd step. On the (22)th[4th] step. On the 5th step. On the (25)th[10th] step. And of course on its own (20th) step.

    If the number of swaps n is odd (that is, n mod 2 = 1), that locker is open, and if that number is even (n mod 2 = 0), the locker is closed. Thus, locker 20, with six toggles, is closed.

  • Christian (unregistered) in reply to Maurits

    Easy. Locker number 1 (once), followed by any prime numbers (twice). From there, it's whatever numbers have exactly 3 factors (incl. themselves).

    I'm fairly sure that 96 is the locker that would be toggled the most incidentally.

  • Gray Pockets (unregistered)
    Comment held for moderation.
  • Ian (unregistered) in reply to Maurits

    Locker one is toggled once. Otherwise, consider that factors ordinarily come in pairs, except in the case of perfect squares, where the squareroot lacks a partner (being its own).

  • Colin (unregistered)
    Comment held for moderation.
  • Joel (unregistered) in reply to Maurits

    Prime numbers.

  • orange_batch (unregistered)

    The objective is to beat the jocks with the right answer, not how that answer is obtained, right?

    In a lazy 7 minutes all I did was program what the jocks were doing.

    DOS batch:

    setlocal enabledelayedexpansion

    for /l %%x in (1,1,100) do ( set locker%%x=0 for /l %%y in (1,1,100) do ( set /a remainder=%%x%%%%y if !remainder!==0 if !locker%%x!==0 (set locker%%x=1) else set locker%%x=0 ) echo locker%%x: !locker%%x! )

  • orange_batch (unregistered) in reply to orange_batch

    K, I didn't read the no brute force part. Whatever.

  • The loonly guy (unregistered)
    Comment held for moderation.
  • ac (unregistered) in reply to Maurits

    The first locker is toggled exactly once. Prime-numbered lockers are toggled twice.

  • Smithd976 (unregistered) in reply to Peter Kelley

    Link exchange is nothing else but it is just placing the other persons weblog link on your page at proper place and other person will also do similar in support of you. fkkgfgcbbcebcgdb

Leave a comment on “Nerds, Jocks, and Lockers”

Log In or post as a guest

Replying to comment #:

« Return to Article