In which we potentially miss the point of the exercise.
Putting the beholders to the side for a bit, let’s talk about practice problems.
Switches
Many years ago, I used to do a lot of programming practice problems (what one coworker calls “katas”, and another calls “interview questions”). I even got into Project Euler for a bit while I was learning Ruby; I worked on each question until I could craft a solution that took less than a second to run, until I started hitting questions where that was actually impossible.
So the other day, I ran into a fairly standard one while brushing up on my Python. You have set of n switches, and some defined sequence of toggling them, and at the end you need to describe the final state of the switches. In this case it was described as follows:
“There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off if it’s on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.”
I know I was supposed to sit down and puzzle out the mathematical sequence (the first bulb is toggled only once, the second twice, the third twice, the fourth three times, hmmm), but it was the end of a long day at the day job and it just wasn’t coming to me.
Brute Force
The brute force solution in this case is to fully simulate the process. Efficiency be darned, let’s just do the thing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 

1 2 3 4 5 6 

Ok, so that works.
Spot the Pattern
Humans are fairly good as spotting patterns, so let’s do a bunch of runs and see what emerges:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 

With m of 11, we see:
1 2 3 4 5 6 7 8 9 10 11 

…and I think I see the pattern. Is it that simple? Let’s tweak the code to show us only when the result changes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 

And with an m of 101, we now see:
1 2 3 4 5 6 7 8 9 10 11 

…and now it’s superclear.
Simplicity
The efficient solution for this version of the switch problem is:
1 2 3 4 5 6 

Is it valid to use the bruteforce simulation to show the way to the elegant math solution? It certainly had me writing more code than if I had just puzzled out the equation. And maybe that’s what I wanted to do anyway.