The Countdown to a Design

This is an analysis and design of a program to allow someone to play the numbers game from the British game show Countdown on a computer. First we’ll review the rules of the game and then we’ll explore techniques for implementing them on a computer.

The numbers game is played with a board of cards containing numbers face down. There are four cards containing either 25, 50, 75, or 100, and twenty cards containing two each of the numbers from 1 to 10. The player specifies six cards to be drawn, between zero and four from the large numbers and the rest drawn from the small numbers. The numbers drawn are revealed and a random number between 0 and 999 is generated. The player then attempts to combine the six numbers using the four arithmetic functions, +, *, -, and /, in order to get as close as possible to the number generated. Ten points are awarded for matching the number exactly, seven points are awarded for an answer between 1 and 5 numbers from the target, and five points are awarded for an answer between 6 and 10 numbers from the target.

The generation of the target is a simple matter since all modern computer languages have a pseudo random number generator which, when combined with some arbitrary parameter such as a portion from the extremely small end of the system clock, provides a truly arbitrary result. For example, if the user were to press the button to generate a target at a time when the nanosecond portion of the system clock contained 345, you might multiply that number by the next pseudo random number returned by the generator and then scale it to be between 0 and 999.

The cards would be handled differently. First, we would declare an array of four big cards and 20 little cards. We would initialize those arrays with the values of the cards that are specified in the rules. Then, we would run an shuffling algorithm, some arbitrary number of times on the big number array, and another arbitrary number of times on the small number array.

The shuffling algorithm would work by generating a pair of distinct random number between zero and three in the case of the big numbers and between zero and nineteen in the case of the little numbers. These numbers would serve as indexes into the respective arrays. For example, you might generate 1 and 3 for the big number array. This would indicate that you should swap the element with an index of 1 with the element with an index of 3. You would repeat this operation some arbitrary number of times generated by calling the random number generator, scaling by a large number and then adding a large number to insure a large minimum number. If you generated a number between 0 and 100 and scaled it by 1000000 and then added 37584 as a minimum you would guarantee that the array would be shuffled between 37584 and 1037584 times. You would shuffle the small numbers in a similar fashion.

Now, given a target number between 1 and 999 and six cards containing numbers that can be used to derive the target, it only remains to give the player 30 seconds to come up with a solution to the problem. But how do we determine if the puzzle even has a solution?

First, we take every combination of the six numbers drawn taken one, two, three, four, five, or six at a time and we combine them using every possible combination of operators. We take each result and store it as either an exact match, a match plus or minus five from the target, a match between six and ten from the target, or else we discard it. Thus, we now know if the puzzle is solvable and we can check the answer that the player gives.

And that is how we analyze and design a program to play the Countdown numbers game. I will use this analysis to actually implement the game. Another time, I’ll analyze and design a program to play the Countdown letters game.


Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.

Pardon My Introspection

It is hard to think when you are tired. To a large degree that is because when you are tired and you close your eyes to think for a moment, you wake up after another moment and realize that you’ve just gone to sleep. It’s not that difficult if you have a topic in mind. The words just seem to flow from your finger tips and there is no need for the misadventure of pausing to think.

Alas, I have beaten the commentary on writing horse to absolute and utter death. I find myself repeating myself about the virtues of Lisp and other similar computer languages. I’ve written the draft of a science fiction story, one that petered out toward the end for lack of an ending. I’ve written a number of posts focused on something that I saw on TV or read somewhere that day.

The problem is, I can’t count on the inspirational article or TV show to supply a topic every day and I find it exhausting to sit and try to think of a topic off the top of my head when I have been up for sixteen hours or more. I say this not so much to complain as to attempt to get at the heart of my problem. I often write an essay to attempt to clarify my thinking on a particular subject so that is what I’m doing now.

Perhaps the ultimate solution is multi-forked. I need to think of a number of topics to write about and keep them handy for days when I can’t think of a topic. I need to start thinking about a topic earlier in the day before I get so tired when it gets close to bedtime. I would love to actually write my blog earlier in the day but that hasn’t worked out so far. There just isn’t enough time in the morning to get in all the various tasks that I’ve set for myself.

Perhaps I am being overly ambitious. Perhaps I need to drop one or two tasks from my daily list. I’ve thought about doing that on numerous occasions but I can’t decide which task to drop. I am beginning to get quicker at getting them all done as my Circadian rhythm shifts. I foresee another struggle on the horizon though when we change to daylight savings time again.

I’m not sure this really helped me find any concrete solutions. It did allow me to get the things that I have already given some thought to down on the page so that I can study them to see if I’ve overlooked any alternatives. But for tonight, I’m going to call it a night and head for bed. Tomorrow is another day.


Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.

Musical Theater Magic

For the last three Sunday nights we have watched a three part series that tracked the history of the musical from the early twentieth century through the present. It reminded me of why I was so moved by musicals and even had ambitions to create rock musicals when I was a teenager.

In my case, other things distracted me and I never followed through with my ambition. What I discovered watching this series was exactly how amazing the intervening years have been. I have remained vaguely aware of many of the productions and even saw the movie versions of some of them, for example Rocky Horror Picture Show and Rent.

The thing that is almost universally true, however, is that a live performance will almost always put a filmed performance in its place. The energy of a live performance, complete with its high points and its occasional blemishes is unmatchable by the canned performance that a movie presents.

I was also reminded of all the wonderful musicals that I had somehow never managed to see, either on the stage or screen. Shows like Cats, Miss Saigon, and Avenue Q. I hope they will be revived by touring companies so that I can catch up on some of them.


Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.

Lisp Fundamentals

NOTE: for all my non-computer friends and readers. If technical topics bother you or make you tense, perhaps todays post is not for you. If you are not given cold sweats when facing a new topic with the flavor of computers or programming, by all means please join me.

There are many things that Lisp, the programming language does right. It never ceases to amaze me and I am going to once again take a few minutes to discuss exactly what some of these things are and why they are so important.

Lisp was not originally conceived of as a programming language. It was invented by John Backus as a notation to enable discussion about Alonzo Church’s lambda calculus.

Lisp is characterized by the structure of its expressions, called forms. The simplest of these is the atom. An atom is a singular symbol or literal and represents a value. For instance, 42 is a numeric literal whose value is the number 42. Similarly, “Hello, world!” is a string literal that represents itself as its value. There are also symbols, which are strings of unquoted characters that are used as fundamental elements of Lisp. There are rules for how a valid symbol is form but for now it is sufficient to know that a symbol starts with a letter and then is composed of zero or more additional characters that can be either a letter, a number, or one of a collection of certain punctuation characters. Since the exact list of other characters varies among the dialects of Lisp, we will leave them unspecified at present.

The other type of form is a list. A list is comprised of a left parenthesis followed by zero or more forms and ending in a right parenthesis. Notice I said forms instead of symbols. The implication here is that you can have lists embedded in other lists as deeply nested as you like. This proves to be an interesting trait as we will soon see.

There is one more fundamentally interesting aspect of Lisp. That is that in a typical Lisp form the first element in a list after the left parenthesis is taken to be an operator. The subsequent elements in the list are considered arguments. The operator is either a function, a macro, or a special form. Macros and special forms, while extremely important and interesting are beyond the scope of this discussion.

That leaves us the operator as function. A typical Lisp function form is evaluated as follows. The first element is examined to determine what kind of form the list is. If it is a function, the rest of the arguments in the list are evaluated and collected in a list and the function is applied to them. If another list is encountered as one of the arguments, it is evaluated in exactly the same way.

For example, consider the expression (+ 4 (* 8 7) (/ (-26 8) 9)). The first operator is +. + is a symbol bound to the function that represents addition. The second item in the list is 4. It is a number that represents itself. The next element in the list is the list (* 8 7). When evaluated, the 8 and 7 are arguments to *, the multiplication function and the value returned by that function is 56. The final element in the top level list is (/ (- 26 8) 9). The / is taken as the division function and is applied to the evaluation of (- 26 8) which is the subtraction function that returns 18. When you divide 18 by 9. you get the value 2. Thus the top level argument list  consists of 4, 56, and 2. When you add all three of those numbers you get 62 which is the value that the expression ultimately returns.

This simple mathematical expression illustrates another fundamental aspect of Lisp. It is expressed as a list form which, given a set of bindings to arithmetic functions expresses a simple program. This identical representation of both data and programming in Lisp, called homoiconicity by the way, is at the heart of much of Lisp’s power. Since Lisp’s programs are indistinguishable from Lisp’s data, they can be manipulated by Lisp programs to great advantage.

Think of it like this. Lisp can, in some sense, think about how it is thinking and modify it as it desires. This is why artificial intelligence investigators like using Lisp so much, it is so similar to the simplified models of intelligence that they are building that the boundary begins to blur.


Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.

Something for Nothing… and Chicks for Free

I am reading a book entitled Imager by L. E. Modesitt, Jr. It is the first book in a series of ten books so far. The premise is that there are certain people in this fantasy world that can manifest things just by thinking about them. It is a wonderful story so far (I’m about a third of the way into the first book). It has gotten me to think about some parallels between the Imagers in that world and programmers in ours.

What does a programmer do if not bring forth a creation solely from his or her mind. And it is more than just a story, although I shudder when I say just a story considering how much I have struggled writing fiction myself. My point though is that programs often have a utilitarian function. They enable us to do things that we couldn’t do without them. Never mind that a program doesn’t have a physical existence, per se. One that can be seen and touched. But it does have perceptible effects upon the world, whether it is on a display screen or in some physical process that it controls through a digital interface.

In the books, Imagurs walked a fine line between being useful to those that didn’t share their gift, and being killed out of fear of what they might do with their awesome talents. Although people don’t fear software developers now, I can see a not so distant future in which they do. And with fear comes persecution.

We need to start teaching our apprentice programmers a code of ethics and holding them to it. If people don’t trust programmers, we stand to face problems over that mistrust, sooner than later. When people realize the power that programmers wield in today’s digital world we better be prepare to allay their fears.

By the way, I highly recommend the book. It is riveting. I’m already looking forward to getting the next volume in the series.


Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.

Groovy! Far Out! Namaste!

I signed up for the test drive of brain.fm. It is a web site that purports to help you focus, meditate, or sleep via music tailored to effect your brain state. I of course signed up for focus. I have often complained that I was having trouble concentrating when I was trying to write, either my morning journal entry or my blog post. I have to report that since I started listening to it right before I started writing this blog post, I have managed to focus on writing it, despite the show on the television. I am wearing headphones.

The music is what might be termed high energy space music, where space music refers to the kind of music played on Hearts of Space. There is a kind of a warble that underlies the somewhat arbitrary melody and driving rhythm. I quite like it. It is varied enough to be interesting without being so complex to distract. I think they’ve accomplished their goal here. I am curious about the meditation track and the sleep induction track.

In the mean time since I am so focused, I intend to take advantage of my state of mind to write a little bit longer post. It will be entirely impromptu due to the fact that I didn’t give it any thought prior to starting to write. To tell the truth, most of my blog posts are written this way. I have some vague idea and I start to riff on it. If I generate questions to which I don’t off the top of my head know the answer, I stop and look them up.

Lately though, I’ve been somewhat agitated and I haven’t followed up with research as much as I would like. It was all I could do just to throw the ideas out there without expending the effort to look up the specific details or studies to support my assertions. This bothered me, but not enough that I did anything about it.

I have studied so called entrainment in the past. The idea is that the brain produces electromagnetic waves at specific frequencies that have been correlated with the subject’s state of mind. The categories of brain waves have been given names from the Greek alphabet, for instance alpha waves, beta waves, delta waves, and theta waves. Each group corresponds to a state of mind.

Studies have been done that indicate that musical content can nudge the brain into one or another of these states. It has been compared to the phenomena where two electronic signals are of nearly the same frequency and one is held rigidly steadfast, the other will drift toward it until they are in unison. This is termed entrainment. I believe this is probably behind the music at brain.fm.

So, I just took a break and went and read on the brain.fm web site about the the science behind their product. It is in fact based on the entrainment research. I think I’m going to give it a try. I would like to be able to get more done when I’m working, sleep more effectively when I’m sleeping, and meditate better when I am meditating. I am not sure about the $150 lifetime program yet but I think $48 for a year is not unreasonable. If I renew my subscription in a year, I may opt for the lifetime plan.


Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.

The Sleep Habits of the North American Homo sapiens

Changing one’s sleep habits is a bitch, not to put too fine a point on it. I recently changed assignments at work and my new group tends to work from approximately 7:30am until around 4:00pm. I’m not required to work those hours but in the spirit of being a team player, I have been working on shifting my circadian rhythm.

I have been getting up early for several years now. I spend the time getting bathed and dressed, doing chores around the house, talking with my wife, taking my dogs for their morning walk, and writing a thousand words in my journal. In order to get all that in and get to the office around 9:00am, I was getting up at 5:30am.

In order to attempt to get to work around 8:00am, I have started getting up at 4:30am. But things aren’t that simple. Getting up an hour earlier means getting to bed an hour earlier, unless I intend to subsist on an hour less sleep a night. I soon discovered that I can’t do with an hour less sleep. Furthermore, I discovered that when I get up at 4:30am, even if I go to bed at 9:00pm instead of 10:00pm, I am not as alert as I am at 5:30am.

This makes it more difficult for me to write in my journal. I find myself nodding off while I’m trying to write. And when I am managing to stay awake, my thought processes aren’t as lively as they are when I get up at 5:30am. This has been slowly improving but I still find myself tired in the morning, tired in the evening when I’m trying to write my blog post, and even tired mid afternoon, around 3:00pm.

I also find that I’m struggling to get anything else done besides my bare minimum routine. I expect it will continue to get better as I keep practicing and my circadian rhythm slowly shifts. Of course when we spring forward, I’ll have to go through this process all over again. I hope everyone gets a good night sleep and wakes up refreshed. I’m certainly going to give it my best try.


Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.