Saturday, August 29, 2020

C plus plus programming and other negative effects of the pandemic

 C plus plus programming and other negative effects of the pandemic

By  Bobby Neal Winters

They say that Isaac Newton invented calculus and put together his theory of gravity during a pandemic.  I learned another computer language and beat the peg puzzle.  Here is how that goes.

You might have seen the peg puzzle at a pizza place or a bar.  It is typically a small piece of wood that is an equilateral triangle if you ignore its thickness.  It has holes drilled in it and the holes are arranged in rows.  In the top row there is on hole; in the second two; this goes on down to the fifth row which contains five holes.  All together, there are 15 holes.

Into these holes, you put 14 pegs.  Usually you fill every hole but the middle hole in the third row.   The idea is that you jump pegs and remove them and you try to get to as few pegs possible.  If you only have one peg, you win.

I’ve got one that was given to me by my father-in-law.  I’ve got it sitting on my computer to my right as I write this.  I don’t remember how long ago he gave it to me, but I do know he’s been dead for 13 years.  I suspect he gave it to me with the expectation that since I am a math guy, I would solve it.  

Well, I have but that has been more than a decade in the making.

The story goes like this.  I cleaned out my garage this summer as a part of my COVID cleaning.  Not being able to go anywhere led to me having a bit of pent up energy.  Couple this with the fact that I decided to get a freezer for my garage to put a beef in (whenever the local butchers catch up that is!) and the garage got cleaned more deeply than it had for a long, long time.  

In my cleaning and rearranging, I found the puzzle.  There it was, unconquered. It had a decade worth of dust and spiderwebs on it.  But it made me remember.

What happens next might not be what you expect.  I put it on the back porch to let it get rained on for a while, and I came into the house and started the process of writing a computer program to solve it.

I’ve been learning C++.  There aren’t many of you out there who really care about computer programming, so I will speak in metaphors.  Have any of you seen the old PBS show “The Wheelwright’s Shop”?  The guy makes furniture, and when he is going to make a chair, his first step is to take an iron fencepost to the forge and make a tool with it that he will eventually use to shape an old log he found in the woods.  

That is what programming in C++ is like: You have to make the tools before you make the pieces, and then you put it together.

This is what I began the process of doing.  For those of you who are interested, I modeled the game with a 15 bit binary number.  There are only certain ways you can move between the numbers.  You program the computer to make the moves and then you stop when you can’t move anymore.  Then you check how many pegs are left.  If there is only one, you win; otherwise, backup and do it again, keeping track of the ways that didn’t work.

For those of you who are interested, there are 89 different ways that the game can end.  There is exactly one of those where a single peg is left.  Moreover, there are a lot of different ways to get to each of the 88 ways that are losers: more than 130 thousand.  

So what I could do would be to memorize the 13 jumps it takes to win, but that doesn’t seem right.   I suppose the best way would be to learn strategy.

That might have to wait for another pandemic.

Bobby Winters, a native of Harden City, Oklahoma, blogs at redneckmath.blogspot.com and okieinexile.blogspot.com. He invites you to “like” the National Association of Lawn Mowers on Facebook. )


2 comments:

Old Nuke 69 said...

I have followed your column off and on in the Pittsburg Morning Sun for quite a while because I'm a native Kansan (Dunlap, if you can find it) and also because my wife is an Okie from Muskogee who is now exiled with me here in Las Vegas, but mostly because I enjoy it. This particular column caught my attention because I took a lot of (mostly applied) math classes on my way to a Ph.D. in Nuclear Engineering and because I am beginning a class about C and C++ on Udemy to occupy myself during my semi-retirement.

Your column stimulated a few of my remaining brain cells to remember two things.

First, Leibnitz is also credited with the invention of calculus, although I don't know if he also did it during a pandemic. There apparently was a lot of controversy over who came up with the ideas first (https://en.wikipedia.org/wiki/Leibniz%E2%80%93Newton_calculus_controversy).

Second, I remember that I took a C/C++ course at Pellissippi State Community College near Knoxville, TN in the late 1990s when I worked at the Oak Ridge National Laboratory, but I don't remember much about C++ because I changed jobs and never put C++ to use. I do remember that it emphasizes object oriented programming (OOP) rather than procedural programming. OOP still seems foreign to me, I suspect because I programmed the traditional way from 1966 onward mostly in Fortran, but also in C and a little assembler or machine language in the early days. The claim is that OOP makes it easier to create reusable software components, but that can also be done with traditional methods. I also can't get past the thought that the C++ compiler simply translates the OOP routines into procedures at the machine level. Programming the peg puzzle seems like a good exercise for learning to think about OOP methods. I think I'll try it if it isn't already an exercise in the Udemy course. Having lived in the South for around 25 years, I remember the peg puzzles from eating at Cracker Barrel where golf tees are used as the pegs. There is now a Cracker Barrel here in Las Vegas, which I may try after we all get through the present Covid-19 restrictions.

Keep up the enjoyable musings.

Bobby Winters said...

Thanks,
Newton was an asshole; he was almost definitely on the Asperger's spectrum. He did get the calculus first, but he just sat on it. He didn't share it even in the Principia. Leibnitz came up with something that could be taught to freshmen.

You are exactly right about OOP. In the end, OOP is about the programming industry instead of programming itself. OOP lends itself to modularity. It is still easy to write bad code no one can read, but there is less of an excuse. The chunks are there.

I am glad you like my column.