## Author(Dick Lane) ## Date(2015-Dec-06) DOCUMENT(); loadMacros( 'PGstandard.pl' , 'MathObjects.pl' ); TEXT(beginproblem()); Context('Numeric'); #### simplify square root of an 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]}" ; } } $z = 360 ; $zS = sqrtShow($z) ; $w = 36 ; $wS = sqrtShow($w) ; $y = 14 ; $yS = sqrtShow($y) ; ## another subroutine could use output from sqrtReduce to produce an answer $ans = Compute( "6 sqrt(10)" ); Context()->texStrings; BEGIN_TEXT \( \sqrt{$z} = $zS \) $BR \( \sqrt{$w} = $wS \) $BR \( \sqrt{$y} = $yS \) $BR \( \sqrt{720} = \{sqrtShow(720)\} \) $PAR \( \sqrt{$z} = \) \{ $ans->ans_rule() \} END_TEXT Context()->normalStrings; ANS( $ans->cmp() ); ENDDOCUMENT();