Sunday, May 14, 2006

My first computer program

One evening in the summer of 1959 (probably during July), I was hanging around PUC's DPL soaking up the ambiance and the reflected glory of the programmers.

An older student, who was running an Intercom 1000 program, exclaimed, "This is the slowest-converging series I've ever seen!" He was printing every tenth partial sum, and after more than sixty printouts (600 terms), the sums were still getting bigger, although more and more slowly.

I asked what that series was, and learned that he was trying to find the limit of the sum of the reciprocals of 2i + 1, for i from one to infinity. I confidently assured him that this series was never going to converge, because it was essentially equivalent to the harmonic series, which was well-known to be divergent, but very slowly divergent.

"Well, it can't be divergent, because it's the solution to a problem that Martin Gardner gives in SCIENTIFIC AMERICAN's Mathematical Games."

Naturally, I asked to see the problem, which was to determine the resistance of an infinite, but regular, network of one-Ohm resistors. I became convinced that he was using the wrong formula. I believed that the correct formula involved a continued fraction, rather than an infinite sum. He was unwilling to admit his error, and when I urged him to at least program my formula and try it, he sulkily handed me the Intercom 1000 manual and said, "Program it yourself."

This was my gentle introduction into the art of programming. I retreated to a corner of the Lab, read the manual, and quickly learned enough to write out a simple loop to evaluate successive approximations to my continued fraction. Having no idea how rapidly the fraction might converge, I decided to print out every tenth approximation, and run the program until that stopped changing.

With some trepidation, I typed my program into the computer, and started it running. It immediately began printing the same number over and over again:
2.73205080875
2.73205080875
2.73205080875
2.73205080875
2.73205080875
2.73205080875
2.73205080875
2.73205080875
2.73205080875
2.73205080875
...
as fast as the typewriter would go.

I assumed that I had made some horrible blunder to cause it to keep printing the same value, so I stopped it and retreated. However, I could find nothing wrong with my program. So I modified it to print out every iteration, rather than every tenth, and discovered that this continued fraction converged really fast, and had reached the limits of double-precision accuracy well before the tenth iteration. So my first try at my first program was correct, but I wound up debugging it anyhow.

Being correct the first time has been rare enough in my programming experience that I would remember this occasion vividly, even if it hadn't been my first program.

Afterword: The next issue of SCIENTIFIC AMERICAN had Martin Gardner's solution to the problem. By looking at the problem in a slightly different way, he came up with a closed-form solution (one plus the square root of three) that produced the same numerical result as my continued fraction, but which could have been calculated with pencil and paper (even without the aid of a table of square roots) in less time than either of us had spent on our programs.

Morals:
A little knowledge is a dangerous thing.
Insight often trumps calculation.