WeBWorK Problems

tool(s) to simplify sqrt(integer)

tool(s) to simplify sqrt(integer)

by Dick Lane -
Number of replies: 0
While designing a problem involving lattice points, I sought a tool to simplify display of distances, e.g., sqrt(360) = 6 sqrt(10).  After quick searches found nothing, I wrote the following (the attached file demonstrates this code in a WeBWorK problem).

####    simplify square root of integer, e.g., sqrt(300) = 10 sqrt(3)
sub sqrtReduce {
    my @Sqrt = (1,@_[0]);
    foreach my $p (2,3,5,7,11,13,17,19) {
        while ( $Sqrt[1] % ($p*$p) == 0 ) {
            $Sqrt[0] *= $p ;
            $Sqrt[1] /= ($p*$p) ;
        }
    }
    return @Sqrt
}

####    LaTeX formatting for output of sqrtReduce
sub sqrtShow {
    my @Root = sqrtReduce(@_[0]) ;
    if ($Root[1] == 1) {
        return $Root[0] ;
    } elsif ($Root[0] == 1) {
        return "\sqrt{$Root[1]}" ;
    } else {
        return "$Root[0] \, \sqrt{$Root[1]}" ;
    }
}

a)  I will welcome suggestions to improve this code.
b)  Does WeBWorK 2.#.# already have a library file with a similar tool?
c)  If not(b), could WW 3.# include a new file to contain tools not already in PGauxililaryFunctions.pl?  I would renew my proposal of an extended gcd discussed in http://webwork.maa.org/moodle/mod/forum/discuss.php?d=2286