Thursday, January 13, 2005

Perfect Numbers

One of the topics I must teach from time to time is how to use JBuilder to build EJB's. This isn't a class on EJB, but rather how JBuilder helps you build them. The problem that arises is how to create an exercise for the students that doesn't rely on specific infrastructure (like a particular database server, network details, etc.). Recently, I found a fun topic.

According to ancient Greek numerology, every number may be clasified as deficient, abundant, or perfect. These classifications are based on the sum of the factors (excluding the number itself). Thus, 9 is deficient (1+3 = 4 < 9), 12 is abundant (1+2+3+4+6 = 16 > 12), and 6 is perfect (1+2+3 = 6 = 6). The perfect numbers are the interesting ones. The exercise is to write an application where the user provides a seed number, and the application finds the next perfect number. Here is a list of the first 8 perfect numbers:

  1. 6
  2. 28
  3. 496
  4. 8,128
  5. 33,550,336
  6. 8,589,869,056
  7. 137,438,691,328
  8. 2,305,843,008,139,952,128

What's nice about this exercise is that it is a nice distraction from the boring plumbing-type coding required to get the application working. This happens several days into the class, so it provides a great outlet. Inevitably, students start doing research and get caught up in this topic. There is a great entry that describes the origins of perfect numbers on the Wikipedia site.

As much as I'd like to take credit for this, it came from an exercise in my first Pascal programming class. Dr. Head had us create lists of numbers that were perfect. On the Apple ][e, finding 496 took about 5 minutes. A simple, non-optimzed Java console application finds the first 4 almost instantly, and takes about 20 minutes to find the 5th. This is always a fun topic.

No comments: