Math/computer science: exponentiation algorithm

By way of finding a power of a number the tutor opens a discussion about algorithms.

Years ago my computer science professor presented the following definition:

An algorithm is a set of steps to solve a problem.

Suppose there wasn’t a dedicated function to calculate ab. Then one would need to devise an algorithm for it. One might commonly imagine a loop that multiplied the base by itself the necessary number of times;eg:


#!/usr/bin/perl

$base=$ARGV[0];
$expon=$ARGV[1];

$ans=1;

for($i=0;$i<$expon;$i++){
$ans*=$base;#means $ans=$ans*$base;
}
print “$base to the exponent $expon is $ans\n\n”;

Perhaps surprisingly, there is another way of evaluating ab that is more efficient. I’ll be introducing that algorithm soon.

Currently we are anticipating heavy rainfall on the west coast. It’s a welcome development after a very dry summer; I’ve high hopes it will squelch the forest fire hazards. Perhaps a coming post will celebrate this change in weather:)

Source:

Grimaldi, Ralph P. Discrete and Combinatorial Mathematics. Addison-Wesley: Don
   Mills, 1994.

Leave a Reply