1to100.js -- a first computer program
I wrote a lot of code this week, and there's much I could share about what I did and how I did it. But only one of the programs has occupied my mind and stayed with me until now, and I want to say something about it.
My five year old has taken to math in a way my wife and I weren't expecting. She has a personality that allows her to stay with a topic for a long time, and patiently learn all that she doesn't know. It's an incredible skill, and has allowed her to move through addition, subtraction, multiplication, calculating squares and cubes of numbers, geometry, etc.
The other day she came to me with a realization:
Dad, I've made a discovery! If you add all the numbers from 1 to 100 together, you get 5050! And I think that it doesn't matter what order you add them in, it will always be the same.
I don't really do much on the computer with my girls, other than allowing them to tear them apart. But this night I decided to show her how we could ask a computer to prove her theory. "Let's write a computer program," I suggested. She was excited, since she knew I wrote programs all day long, and was eager to do it herself.
So how do you write a program? I was fascinated to see that my daughter's way was to take out a piece of paper and draw a picture. She drew the numbers in groups, with circles around each, which reflected the way she had solved it herself, adding 1-10, then 11-20, and so on, before adding each of these together (a giant circle enclosing the other circles). It was a beautiful notation, and rather than write it how I would, I set about implementing her solution. In a few minutes we had it working: Total = 5050.
Dad, what if we multiplied instead? Can your computer do that? But I don't want to lose the adding!
One more line of code, and she was thrilled to see another number get printed under 5050. A really big one: 9.33262154439441e+157. Now we had to discuss what really, really big numbers look like, and how to represent them.
It was one of the more fulfilling programming tasks I've done in a long time, and the value of the program was inversely proportional to the length of the code. Of course we wrote it in JavaScript, and the code to implement this is left to the reader as an exercise, since the exercise is worth more than the result. The more important code I want to share now is that of time with one's daughter thinking about numbers.