Dan and I have come up with a total of 5 methods of programming this little problem. There are undoubtedly a plethora of good solutions. I gave this to my programming class to do in Small Basic. SB is perfect for this kind of little assignment, it does simple graphics without any fuss. I have three kids in the class. Two solutions were very similar to the state variable solution, especially since I showed one kid what a state variable did and how it worked and he shared the idea to the other two kids. I expected the same basic solution from all three kids. Kid number three had to be different. He turned in the code and I stared. “What the heck did he do?!” It took me a while to figure out he had come up with a recursive solution. Very clean, very unique. I love it.
If you are going to teach programming the cookie cutter has to go out the window. In math there is usually a “best” method to reach the solution. In programming there are good solutions and bad solutions, all of which will work. And sometimes there are solutions that are just better than anything you may have thought of. Smart kids in math are manageable, you know where they are going and what tools they have to work with to get there. Smart kids in programming find tools you did not know existed and they will use them in ways you never thought of. I love it.