As a first assignment, Dr. Karron asked for the creation of this page. One of the stipulations was that it should contain some kind of dynamic content generate by a program. I have decided to implement the algorithm for determining a cube root decribed by Martin Gardner on page 157 of his book Mathematics Magic and Mystery. (New York: Dover Publications, Inc., 1956.) It must be understood that the algorithm is intended to be used by a "lightning calculator " ( a person with the ability to perform mathematicalcalculations very quickly) in front of a live audience, and consequently is a silly thing to have a computer do, but it is more interesting than "Hello World" :-). This algorithm will only work with the set S : { x | x ∈ Z and 1 ≤ x ≤ 100 } The "lightning calculator" will have memorized the cubes of the integers 1 through 10. He asks a member of his audience to pick an integer between 1 and 100, cube it, and announce the result. He then finds the answer by making use of the fact the cubes of all integers between 1 and 10 end in the same integer as the one that was squared with exception of 2,3,7,and 8. For example, 4 cubed is 64. Both these numbers end in four. 5 cubed is 125, 6 cubed is 216, etc.. The cubes of 2,3,7, and 8 end with the integer that represents the difference between 10 and their cube roots. For example, 2 cubed is 8, and 10-8 is 2. 7 cubed is 343. 10-3 is 7.
Now the "ligthning calculator" removes the last three digits from the number he was provided with and finds the two sequential cubes of integers between 1and 10 that the modified number falls between. For example, if the number given to our "lightning calculator" is 54872, the modifed number (after removing the last three digits) is 54. 54 falls between 27 and 64 which are the cubes of 3 and 4 respectively. Now the cube root is found by determining the last digit and then the first. The last digit can be determined using the information we examined concerning which integer cubes will end in given a root between 1 and 10. In this case the cube 54872 ends in 2. Therefore the last digit of its root must be 8. The first digit is determined by choosing the smaller of the two roots whose cubes delineate a range that includes the modified number. In this case those roots were 3 and 4, so we choose 3. The cube root of 54872 then is 38.
Below is the annotated code in Perl that implements the algorithm.
#!/usr/local/bin/perl
###############################################################################
# cube_root.cgi
# by Peter Cline 02-10-2002
# Implements Martin Gardner's description of how one can immediately determine
# a cube root given a cube of an integer between 1 and 100
# The program assumes that the number provide is indeed a cube. Therefore if
# a number which is not a cube is given,the program will return the root of the
# nearest cube. This is consistent with how a person would perform, unless
# one took the time to memorize all 100 cubes
###############################################################################
use strict;
use CGI qw(:standard);
my $q = new CGI;
print header;
my $cubed = $q->param("cubed"); #get user provided cube from cgi parameters
my @cubes = qw(0 1 8 27 64 125 216 343 512 729 1000); #here the program "memorizes" the first 10 cubes.
# 0 is included because Perl uses 0 indexed arrays
my $root = "";
if ($cubed > 1000000) { #check to see if the number is bigger then the cube of 100
print<<HTML; #if so, an error screen is printed.
<html>
<head>
<title>Error Page</title>
</head>
<body bgcolor="#ffffff">
<h3>Oops,your number was too large!</h3>
<p>I'm sorry but I am only able to deal with cubes of numbers between 1
and 100. The number you gave me is too large!. Please try again. If you
use a number in the proper range, I promise I won't disappoint.</p>
<form method="POST" action="/cgi-bin/cube_root.cgi">
If you please, pick an integer between 1 and 100, cube it and put the result here:
<input type="text" name="cubed" size="10" maxlength="10">
<input type="submit" value="Find the Cube Root!">
</form>
<a href="/D.BioInformatics_Spring2002/D.Students/peterc">Return to My Homepage</a>
</body>
</html>
HTML
} else { #otherwise we calculate the cube
if ($cubed =~ /([145690])$/) { #if a cube ends in 0,1,4,5,6 or 9 so will its root
$root = $1;
} elsif ($cubed =~ /([2378])$/) { #if a cube ends in 2,3,7 or 8 its root ends in 10 - last digit of cube
$root = 10 - $1;
}
if (length $cubed > 3) {
$cubed = substr ($cubed,0,length($cubed) - 3); #create modified number by removing the last three digits
for (1..10) { #find 2 cubes between which modified number falls and prepend
#root of smaller cube to the root we are determining
my $next = $_ + 1;
if ($cubed > $cubes[$_] and $cubed < $cubes[$next]) {
$root = $_.$root;
last;
}
}
}
print<<HTML; #print confirmation page
<html>
<head>
<title>Success Page</title>
</head>
<body bgcolor="#ffffff">
<h3>I have an answer for you</h3>
<p>The cube root of the number you provided is $root. If you'd like to try again, you may do so below.
Otherwise, use the link below the form to return to my homepage.</p>
<form method="POST" action="/cgi-bin/cube_root.cgi">
If you please, pick an integer between 1 and 100, cube it and put the result here:
<input type="text" name="cubed" size="10" maxlength="10">
<input type="submit" value="Find the Cube Root!">
</form>
<a href="/D.BioInformatics_Spring2002/D.Students/peterc">Return to My Homepage</a>
</body>
</html>
HTML
}