# WeBWorK problem written by Carl Yao
# Portland Community College
#
# Simplify radicals with variables.
#
# Last edited: Carl Yao 7/26/16
#
# ENDDESCRIPTION

## DBCCSS('8.EE')
## DBsubject(Algebra)
## DBchapter(Basic Algebra)
## DBsection(Exponents and Radicals)
## Institution(PCC)
## Author(Alex Jordan, Carl Yao, Chris Hughes)
## MO(1)
## KEYWORDS('simplify','radical','exponent')

 
DOCUMENT();

loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "PGML.pl",
  "contextFraction.pl",
  "contextLimitedRadical.pl",
  "parserRadioButtons.pl",
  "PGcourse.pl"
);

######################################

TEXT(beginproblem());
Context("Numeric");
$var = list_random('x','y','z','t','r','m','n','a','b','c');
Context()->variables->are($var=>'Real');
$showPartialCorrectAnswers = 1;

do {
   $a = random(2,5,1);
   $b = random($a+1,8,1);
} until (gcd($a,$b)==1);


#Create a hash and its inverse that randomizes the order of the four choices.
%scramble = ();
%inverse = ();
for my $i (0..3) {
  do {$temp = list_random(0..3)} until !(grep(/^$temp$/,values %scramble) );
  $scramble{$i} = $temp;
  $inverse{$temp} = $i;
};

#Create 4 choices
@choices=();

$choices[$scramble{0}] = "\frac{1}{\sqrt[$b]{$var^{$a}}}";

$choices[$scramble{1}] = "-\sqrt[$b]{$var^{$a}}";

$choices[$scramble{2}] = "\frac{1}{\sqrt[$a]{$var^{$b}}}";

$choices[$scramble{3}] = "-\sqrt[$a]{$var^{$b}}";

$radio = RadioButtons(
  ["A","B","C","D"],
  $ALPHABET[$scramble{0}], # correct answer
  order => ["A","B","C","D"], separator=>$BR.$SPACE.$SPACE.$SPACE
);

#####################################################

BEGIN_PGML

Convert [` [$var]^{-\frac{[$a]}{[$b]}} `] to a radical.

END_PGML

Context()->texStrings;
BEGIN_TEXT

$PAR
$SPACE$SPACE$SPACE A: $SPACE$SPACE$SPACE \( $choices[0] \)
$PAR
$SPACE$SPACE$SPACE B: $SPACE$SPACE$SPACE \( $choices[1] \)
$PAR
$SPACE$SPACE$SPACE C: $SPACE$SPACE$SPACE \( $choices[2] \)
$PAR
$SPACE$SPACE$SPACE D: $SPACE$SPACE$SPACE \( $choices[3] \)
$PAR
$SPACE$SPACE$SPACE\{ $radio->buttons() \}

END_TEXT
Context()->normalStrings;

############################################################

ANS( $radio->cmp() );

##############################################

BEGIN_PGML_SOLUTION

First, recall how to handle negative exponents:

    [`` x^{-n} = \frac{1}{x^n} ``]

Fraction exponents can be converted into radicals this way:

    [`` x^{\frac{m}{n}} = (x^{m})^{\frac{1}{n}} =  \sqrt[n]{x^{m}} ``]

For this problem, we have:

    [`` 
\begin{aligned}[t]
   &\phantom{{}=}[$var]^{-\frac{[$a]}{[$b]}} \\
   &= \frac{1}{[$var]^{\frac{[$a]}{[$b]}}}\\
   &= \frac{1}{([$var]^[$a])^{\frac{1}{[$b]}}} \\
   &= \frac{1}{\sqrt[[$b]]{[$var]^[$a]}} 
\end{aligned}
    ``]

The correct answer is [`[@ $radio->correct_ans() @]*`].

END_PGML_SOLUTION

######################################



ENDDOCUMENT();
