Saturday, May 26, 2018

Things I Learned En Route to Looking Up Other Things...

...was the title of a regular feature written by the newspaper columnist Sydney J. Harris. I suppose were he alive today, Harris might alter the title to better reflect the era in which we live, perhaps something like: "Information Unintentionally Gleaned while Perusing the Internet." A less thoughtful writer might call a similar column: "Useless Shit I Found Out While Surfing the Web".

I don't know if any knowledge can really be considered useless, as one never knows where that knowledge might lead. My daughter and I are currently reading a book together called The Lost Track of Time by Paige Britt which is about a creative young woman named Penelope, around my daughter's age, whose sense of wonder at the world is all but crushed by her controlling mother who insists that every waking moment of the child's life be put to "good use." No foolish, or unproductive activity like daydreaming or even writing down her thoughts in her journal was tolerated.

Of course her mother had good intentions as Penelope's dad kept reminding her, she only wanted the best for her child. What that meant was doing everything she could to insist that her daughter prepare herself for success in a very competitive world. So far, we're only a few chapters into the book but I can see where it's going, my guess is that eventually the mother will come to her senses, lighten up, and recognize that there is indeed something to be said for letting the mind wander at times. After all, some of the greatest ideas in history came to people when they least expected it.

Anyway, if you've read the book, please don't spoil it for me.

In the past month I've let my mind wander to a place it's been before. Despite working on a number of projects that deserve my full attention, including this blog, I've been obsessed  lately with computer programming. If you don't know me, that may seem like hardly a pointless obsession, after all, computer programming is a useful and well-compensated profession. However at this stage in my life, I'm hardly a candidate for employment in that industry, having absolutely no working experience in the field, and having to compete with kids nearly a third my age, just a little older than my son.

It was in fact my boy's enrolling in a computer programming class in high school that inspired me to brush off my old computer books, well the ones that I didn't weed out of my admittedly overstocked collection of volumes on the subject. For me, computer programming has been an avocation, an obsession that has kept me up at night so often that I truly understand why they chose to name not one but two of the most popular programming environments around after the substance most frequently used by programmers to keep them from falling asleep at their keyboards, Java.

Since programming for me is something I pick up sporadically, often years after I put it aside, I have to re-learn the basics, but even at my advanced age it comes back fast enough. This iteration (to borrow a well used programming term) of my obsession was inspired by the need to help my son get out of his class with an acceptable grade, since he has not inherited this passion of mine, at least not yet. Despite that I completely understand his predicament. His frustrations remind me of my own many many years ago when a small section on programming was included in a math class in high school. That was back in the days before personal computers, when all we had at school were terminals into which we would type our code, in the BASIC language if my memory serves, which would then be output onto punched paper tape. The tape then had to be mailed to a local university, in our case Northwestern, where it would be fed the school's mainframe computer, the kind that in those days would take up a good sized room. The result would be returned to us printed out on a sheet of paper which more often than not, instead of producing say, a nice list of all the prime numbers between one and one thousand, informed us that our program contained an error, or dozen, and would be terminated. The process from typing in the code to being informed of our failure took one week. Then we would repeat the process until we got a successful result. (borrowing some common BASIC  key words). Sometimes our efforts would result in an endless loop. Clearly this process did not put the computer in its best light, and no explanation by the teacher that we were unlocking the vast potential of the power of computers could make up for the fact that we could go through every number between one and one thousand, do the arithmetic by hand using long division, and still come up with all the primes to one thousand far quicker than we could with this "new and improved" system.

It's much the same in my son's class. Cutting out the middleman makes the feedback much quicker, but that doesn't lessen the sting of being told by a dumb machine that you screwed up over and over and over again. Not to mention the fact that kids today spend untold hours in front of a computer screen, creating with a minimum amount of effort or skill, a whole world, or at least a virtual representation of it, before their eyes. I feel for the teachers who have to convince their young students that there is much value to be had in learning to write several lines of code just to make a simple message like "hello world" display on the screen.

Anyway, I think it takes a special type of person to cut through all the tedium and initial failure, in order to be able to write successful code. Those people, and I'm including myself only to a small degree as I'll never fully measure up to the really serious ones, are derisvely called geeks, a term they, (we), have proudly adopted. What other person would spend countless sleepless nights just to figure out things like how to make a dot move from one end to the screen, to the other on its own? I've been there and done that.

For the last month, while it would have behooved me to concentrate on other things, I've been contemplaing algorithms that sort numbers, have struggled to write my own, and beat my head against a tree trying to wrap it around the subject of just how recursion works. I think I finally got it. And much like making a successful golf shot, the excitement. bordering on ecstasy, of finally getting an algorithm to work the way you want it to, keeps you coming back despite the inevitable frustrations of the process.

If you know anything about programming, from what I just told you, you know that I'm still at a pretty basic level. Armed with that knowledge however, I remain undaunted, convinced that at the very least, the mental exercise is helping keep the few brain cells I have left, alive and well.

Last week I asked my son what they were doing in programming class. He said they were learning about double arrays and that the teacher mentioned that these curious things were useful in several applications including writing the code for a tic-tac-toe game. Then I asked him if tic-tac-toe would be their next project. I thought it would be a great exercize to teach computer logic if not being a particularly challenging game in its own right. (Writing a program to play chess would be much too achallenging for a beginner class).

He told me there was no tic-tac-toe program in his future, so I decided to work on the problem myself. Without going into details, the gist of writing a tic-tac-toe program where the computer makes strategic moves to try to win, is have the computer figure out every conceivable sequence of moves. rate them, then choose the sequence that has the highest chance of winning. In order to do that you have to first write a sub-program that will calculate all the possible combinations for a series of numbers starting with nine, the number of spaces in a tic-tac-toe game, then reduce that number by one after each space is taken up by an X or an O.

It's easy to figure how many possible combinations, or permutations (if not the program to actually generate them), that you can make out a sequence of a given number objects. You take the number of objects (n), and multiply it by (n-1). Then you multiply that product by (n-2)  then (n-3) and so on, reducing the number subtrated by one until you reach 2. That operation is called a factorial and the symbol for the operation is the exclamation point (!) following the number. So 4! = 4 x 3 x 2, which equals 24. That means there are 24 different ways or permutations in which you can display 4 objects.

According to that formula, 5! = 120 and 6! = 6000. The word exponential is used as a metaphor for a rapid increase of something. But as you can see, factorials get really big, really really fast, in fact exponentially faster than exponents. For example, the factorial of nine,  or 9!, the number of possible moves in a game of tic-tac-toe, is 362,880!

I first understood this years ago in high school after I spent what was for me a small fortune on a scientific calculator. I was well versed in trigonometry at the time but didn't know what the x! button meant. So I tried it out and discovered that most of the numbers I applied it to resulted in the error message. The reason as you can probably guess is that very soon, the result of the calculation was greater than the number of digits available on the calculator. The factorial of 13 is 6 billion something, requiring 10 digits. If the calculator only had 10 digits available to it, 13! was the highest factorial the calculator could handle, as scientific calculators in the day, or at least mine, ironically did not provide scientific notation.

With that in mind I tried to conceive yesterday on the walk home from the train, what the factorial of 52 is, the number of playing cards in a standard deck of cards. In that vein I wondered what the probability was of shuffling a deck of cards and having them come out in the same order they were when the new pack was first opened. I asked my daughter what she thought the number would be. A couple thousand she said. My son guessed something in the millions. At that point I didn't know the answer but I assured them that the number of possibilities was far greater. However at the time, I had no clue how inconceivably large that number is.

This morning on the way to work, I remembered the question then googled "factorial of 52." The word astronomical doesn't even begin to describe how large the number is. In the words of the writer of this site specifically on how large 52! is:
most numbers that we already consider to be astronomically large are mere infinitesmal fractions of this number.
According to the scientific calculator found on the web, 52! = 8.0658175e+67

In layman's terms, how big is that number?

From that same website, the number is:

80,658,175,170,943,878,571,660,636,856,403,766,975,289,505,440,883,277,824,000,000,000,000

All those zeros at the end just mean that the number is so large, you'd have to write special code to calculate the number to complete precision. In other words, the number of possible arrangements of playing cards in a 52 card deck is the number above, give or take about 500 billion, which compared to the actual number, is a mere drop in the bucket.

So how big exactly is 8.0658175 times ten to the 67th power?

Again from the site:
Start a timer that will count down the number of seconds from 52! to 0. We're going to see how much fun we can have before the timer counts down all the way.
Start by picking your favorite spot on the equator. You're going to walk around the world along the equator, but take a very leisurely pace of one step every billion years. The equatorial circumference of the Earth is 40,075,017 meters. Make sure to pack a deck of playing cards, so you can get in a few trillion hands of solitaire between steps. After you complete your round the world trip, remove one drop of water from the Pacific Ocean. Now do the same thing again: walk around the world at one billion years per step, removing one drop of water from the Pacific Ocean each time you circle the globe.The Pacific Ocean contains 707.6 million cubic kilometers of water. Continue until the ocean is empty. When it is, take one sheet of paper and place it flat on the ground. Now, fill the ocean back up and start the entire process all over again, adding a sheet of paper to the stack each time you’ve emptied the ocean. 
Do this until the stack of paper reaches from the Earth to the Sun. Take a glance at the timer, you will see that the three left-most digits haven’t even changed. You still have 8.063e67 more seconds to go. 1 Astronomical Unit, the distance from the Earth to the Sun, is defined as 149,597,870.691 kilometers.So, take the stack of papers down and do it all over again. One thousand times more. Unfortunately, that still won’t do it. There are still more than 5.385e67 seconds remaining. You’re just about a third of the way done.
Unfortuantely I don't have that much time on my hands. Less than five steps into our journey as described above, the sun having nearly consumed its fuel source, will begin expanding into a red giant, eventually extending beyond the earth's orbit and consuming our beautiful planet. The one question I do have is this, will Trump still be president then?

Certainly by that time human beings, if  inded there indeed is still such a thing, will have figured out a way to leave the planet and the solar system but there is another bigger threat only four steps into our journey. At that time it is expected, and astronomers have gotten pretty good at predicting such things, that the galaxy in which we reside, we call it the Milky Way, will collide with the Andromeda galaxy, currently about 2.5 million light years or if you prefer. 1.47 times 10 to the 19th power or 14,700,000,000,000,000,000 miles away, That may seem like an awfully big number but you'd have to multiphy that number by 5.4869507 times ten to the 47th power to equal the number of variations you can make out of a simple deck of cards.

Now imagine the smallest thing you can imagine, atoms (ok there are components of atoms that are obviously smaller than atoms but play along with me here), and the largest thing you can imagine, the universe.

It is estimated that there are between 1 x 10 to the 78th power and 1 x 10 to the 82nd power atoms in the known universe. Now take the factorial of the number 60. It is 8.321 times ten to the 81st power, about equal to the number of atoms in the universe..That means if you had a deck of 61 cards, (not sure what kind of game you'd play with 61 cards), you could arrange them in more ways than there are atoms in the universe.

What all this means I have no idea. All I know is that I had better not try to plug the number 52 into my version of  Heap's permutation algorithm, which generates all the possible combinations of the number you give it. At a billion calculations per second (an extremely generous estimation of my computer's performance) the program would take approximately, hummm let's see:

22,405,049,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000 hours to complete.

That's give or take way more than a few trillion hours which is about the amount of time physicists predict, when time itself will cease to exist.

Talk about losing track of time.

No comments: