WeBWorK Problems

Error traced to gcd( ) using more than 2 arguments?

Error traced to gcd( ) using more than 2 arguments?

by tim Payer -
Number of replies: 2

Hello webwork folks,


I was under the impression that we could use more than two arguments for the gcd( ) command. But I am finding that for particular seed values in one of my homework problems that this creates the following error statement:


Illegal modulus zero at line 155 of [PG]/macros/PGauxiliaryFunctions.pl

I have traced the variable values and can reproduce the error using just the numeric constants that the variable used:


$gc3 = gcd(14,14,7,14);


If I comment this line of code out and replace it with the following:


$gc3 = 7;


Then the homework problem works fine.


Any ideas as to why this glitch is occurring?


I am attaching the code below.


Thanks so much for any guidance...



A bad seed value is 1606.  

and this seed returns the following initial values

$a = 2;

$b = 1;

$c = 2;

$x1 = 7;

$y1 = 7;


A good seed value is 1607.


## DESCRIPTION

## Calculus

## ENDDESCRIPTION


## Tagged by tda2d


## DBsubject(Calculus - multivariable)

## DBchapter(Differentiation of multivariable functions)

## DBsection(Partial derivatives)

## Institution(Dartmouth)

## Level(2)

## TitleText1('Basic Multivariable Calculus')

## AuthorText1('Marsden, Tromba, Weinstein')

## EditionText1('3')

## Section1('2.6')

## Problem1('')

## KEYWORDS('tangent' 'slope')

## Library/Dartmouth/setMTWCh2S6/problem_2


DOCUMENT();

loadMacros("PG.pl",

          "PGbasicmacros.pl",

          "PGchoicemacros.pl",

          "PGanswermacros.pl",

          "PGauxiliaryFunctions.pl",

          "PGgraphmacros.pl",

          "MathObjects.pl",

          "Dartmouthmacros.pl");


## Do NOT show partial correct answers

$showPartialCorrectAnswers = 1;


## Lots of set up goes here

$x1 = random(1,8,1);

$y1 = random(1,8,1);


#$x1 = 7;

#$y1 = 7;


$a = random(1,4,1);

#$a =2;

$a_x = clean_scalar_string($a, "x");


$b = random(1,4,1);

#$b =1;

$b_y = clean_scalar_string($b, "y");


$c = random(1,4,1);

#$c =2;

$c_xy = clean_scalar_string($c, "xy");


$ga = gcd($a,2);

$gb = gcd($b,2);

$gc = gcd($c,2);



$na = $a/$ga;

$nb = $b/$gb;

$nc = $c/$gc;

$da = 2/$ga;

$db = 2/$gb;

$dc = 2/$gc;


if($da ==1){

$fa=$na;

$faa = $na;

}

else{

$fa="\frac{$na}{$da}";

$faa = $na/$da;

}


#$fa=$na;    # test value


if($db ==1){

$fb=$nb;

$fbb = $nb;

}

else{

$fb="\frac{$nb}{$db}";

$fbb =$nb/$db;

}


#$fb=$nb; # test value


if($dc ==1){

$fc=$nc;

$fcc = $nc;

}

else{

$fc="\frac{$nc}{$dc}";

$fcc = $nc/$dc;             

}


#$fc=$nc;  # test value


$fa2 = 2*$na/$da;

$fb2 = 2*$nb/$db;

$fc2 = 2*$nc/$dc;


$fcy = $fc2*$y1;

$fcx = $fc2*$x1;


$rad3 = $a*$x1+$b*$y1;   # initial roots

$rad4 = $c*$x1*$y1;      # initial roots

$rad1 = $c*$x1*$y1;           # reduced roots

$rad2 = $a*$x1+$b*$y1;     # reduced roots



$rads = $rad4**0.5;  # the square root of the radicand.

$i = floor($rads);   # rounding the square root up to the nearest integer.

$cc1 =1;  # Needed to replace coefficient if there is no reduction: $cc1*$rat1

do {

$j = $i**2;       #We square the counter, looking to pull the largest square from the root.

$gc7 = gcd($j,$rad4);   #Find the gcd between the j (the i counter squared) radicand.

$i = $i-1;

}until ($gc7 == $j);

$cc1 =$i+1;    # Restoring the counter value of i for the pulled factor.

$rad1 = $rad4/$j;  # Reducing the radicand of the original root


#$rad1 = 2;  # test value

#$cc1 = 7;   # test value


$rads = $rad2**0.5;  # the square root of the radicand.

$i = floor($rads);   # rounding the square root up to the nearest integer.

$cc2 =1;  # Needed to replace coefficient if there is no reduction: $cc1*$rat1

do {

$j = $i**2;       #We square the counter, looking to pull the largest square from the root.

$gc7 = gcd($j,$rad2);   #Find the gcd between the j (the i counter squared) radicand.

$i = $i-1;

}until ($gc7 == $j);

$cc2 =$i+1;  # Restoring counter

$rad2 = $rad2/$j;   # Reducing the radicand of the original root


#$rad2 = 21;   # test value

#$cc2 = 1;          # test value



$fa3 = $fa2*$cc1;

$fc3 = $fcy*$cc2;

$fb3 = $fb2*$cc1;

$fd3 = $fcx*$cc2;


#$gc3 =7;           #  test values

#$gc3 =gcd(14,14,7,14);    #  test values


$gc3 =gcd($fa3,$fc3,$fb3,$fd3);    # Culprit number 1..

$fa4 = $fa3/$gc3;

$fc4 = $fc3/$gc3;     

$fb4 = $fb3/$gc3;

$fd4 = $fd3/$gc3;


$cr1 = $fcc*$x1;

$cr2 = $fcc*$y1;

$q1 = $fa4*$fb4;

$q2 = $fa4*$fd4;

$q3 = $fc4*$fb4;

$qm =$q2-$q3;

$q4 = $fc4*$fd4;

$q5 = $fb4*$fb4;

$q6 = $fb4*$fd4;

$q7 = $fd4*$fb4;

$q8 = $fd4*$fd4;   

$zab = $q5*$rad1;    # used in solution.

$zabb = $q1*$rad1;   # used in solution.

$zcd = $q4*$rad2;      # used in solution.

$zdg = $q8*$rad2;     # used in solution.

$gd1 = $zab - $zdg;   # used in solution.

$gn1 = $zcd - $zabb;   # used in solution.



$gc9 =gcd($gn1,$qm,$gd1);  # used in solution.

$gn2 =$gn1/$gc9;    # used in solution.

$qm2 =$qm/$gc9;    # used in solution.

$zrb2 =$qm/$gc9;    # used in solution.??

$gd2 =$gd1/$gc9;   # used in solution.

$gn3 =-$gn1/$gc9;    # used in solution.

$zrb3 =-$qm/$gc9;    # used in solution.

$gd3 =-$gd1/$gc9;   # used in solution.


$rootp =$rad1*$rad2;   # radicand product.

$qroot = Formula("$qm*sqrt($rootp)");


if($zrb3 ==1){

$qroot = "\sqrt{$rootp}";

$qrootc =$rootp**0.5;

}

else{

$qroot = "$zrb3 \sqrt{$rootp}";

$qrootc =$zrb3*$rootp**0.5

}


$ans4 =Compute("($gn3+$zrb3*($rootp)**0.5)/$gd3");

$ans5 = sprintf("%0.4f",$ans4);



$num = $a/(2*sqrt($a*$x1 + $b*$y1) ) + $c*$y1/(2*sqrt($c*$x1*$y1) );

$denom = $b/(2*sqrt($a*$x1+$b*$y1) ) + $c*$x1/(2*sqrt($c*$x1*$y1) );


$yp = - $num/$denom;


#

## Ok, we are ready to begin the problem...

##

TEXT(beginproblem());



BEGIN_TEXT

$BR

Find the slope of the tangent line to the curve

$BR

\(\displaystyle \sqrt{$a_x +$b_y}  + \sqrt{$c_xy} =

   \sqrt{\{$a*$x1+$b*$y1\}} + \sqrt{\{$c*$x1*$y1\}} \)

at the point \( ( $x1,$y1 ) \). $BR


The slope is \{ ans_rule(70) \}.


END_TEXT


ANS(num_cmp($yp));


#Context()->texStrings;


BEGIN_SOLUTION


To find the derivative by implicit differentiation we must apply the notation of \(\frac{dy}{dx}\) to emphasize that the derivative is applied with respect to the variable of \(x\). The terms holding only \(x\) variables or constants will be treated with our simplified prim tic application of the derivative. But for any term that holds a variable that is not \(x\), say for example \(y\), then the prime tic we apply for the derivative will take on \(\frac{dy}{dx}\)  to permit the derivative to be applied to \(x\) implicitly through the variable of \(y\). $BR

$BR

The derivative will be a function of two variables, \(x\) and \(y\). We can find the slope of the tangent line by evaluating the derivative, \(m = \frac{dy}{dx}\) at  \(x = $x1\) at  \(y = $y1\). $BR

$PAR

\(\begin{aligned}&\\  

\displaystyle \sqrt{$a_x +$b_y}  + \sqrt{$c_xy} &=

   \sqrt{$rad3} + \sqrt{$rad4}\\  

\displaystyle ($a_x +$b_y)^\frac{1}{2}  + ($c_xy)^\frac{1}{2} &=

   ($rad3)^\frac{1}{2} + ($rad4)^\frac{1}{2}  && \text{Begin by converting the square roots to rational exponents.}\\  

\displaystyle \left(($a_x +$b_y)^\frac{1}{2}\right)'  + \left(($c_xy)^\frac{1}{2}\right)' &=

   \left(($rad3)^\frac{1}{2}\right)' + \left(($rad4)^\frac{1}{2}\right)'  && \text{Apply the prime tics for the derivative.}  

\end{aligned}\)  $BR

$BR

Provide prime tics for the chain and product rules and include a \(\frac{dy}{dx}\)  for each prime tic on \(y\). $BR

$BR

\(\displaystyle{\left(($a_x +$b_y)^\frac{1}{2}\right)'\left($a_x' +$b_y' \cdot \frac{dy}{dx}\right)  + \left(($c_xy)^\frac{1}{2}\right)' \cdot $c \left(x' \cdot y + x \cdot y' \cdot \frac{dy}{dx}\right)  = \left(($rad3)^\frac{1}{2}\right)' + \left(($rad4)^\frac{1}{2}\right)'}\) $BR

$BR

Apply the derivative at each prime tic.$BR

$BR

\(\displaystyle{\frac{1}{2}($a_x +$b_y)^\frac{-1}{2}\left($a +$b \cdot \frac{dy}{dx}\right)  + \frac{1}{2}($c_xy)^\frac{-1}{2}\cdot $c \left(y + x \cdot \frac{dy}{dx}\right)  =

   0}\) $BR

$BR

Distribute the constants of  \(\frac{1}{2}\)  and and convert the negative powers into denominator square roots. $BR

$BR

\(\displaystyle{ \frac{1}{ \sqrt{$a_x +$b_y}}\left( \frac{$a}{2} +\frac{$b}{2}  \cdot \frac{dy}{dx}\right)  + \frac{1}{\sqrt{$c_xy}} \frac{$c}{2} \left(y + x \cdot \frac{dy}{dx}\right)  =

   0}\) $BR

$BR

Reduce. $BR

$BR

\(\displaystyle{ \frac{1}{ \sqrt{$a_x +$b_y}}\left( $fa +$fb  \cdot \frac{dy}{dx}\right)  + \frac{1}{\sqrt{$c_xy}} $fc \left(y + x \cdot \frac{dy}{dx}\right)  = 0}\) $BR

$BR

Distribute through the quantities: $BR

$BR

\(\displaystyle{ \frac{$fa}{ \sqrt{$a_x +$b_y}} + \frac{$fb}{ \sqrt{$a_x +$b_y}} \cdot \frac{dy}{dx}  + \frac{$fc y}{\sqrt{$c_xy}} + \frac{$fc x}{\sqrt{$c_xy}} \cdot \frac{dy}{dx}  = 0}\) $BR

$BR

Multiply each term by 2 and isolate the \(\frac{dy}{dx}\) terms: $BR

$BR

\(\displaystyle{   \frac{2 \cdot $fb}{ \sqrt{$a_x +$b_y}} \cdot \frac{dy}{dx}   + \frac{2 \cdot $fc x}{\sqrt{$c_xy}} \cdot \frac{dy}{dx}  = -\frac{2 \cdot $fa}{ \sqrt{$a_x +$b_y}} - \frac{2 \cdot $fc y}{\sqrt{$c_xy}}}\) $BR

$BR

Reduce the minor fractions.$BR

$BR

\(\displaystyle{   \frac{$fb2}{ \sqrt{$a_x +$b_y}} \cdot \frac{dy}{dx}   + \frac{$fc2 x}{\sqrt{$c_xy}} \cdot \frac{dy}{dx}  = -\frac{$fa2}{ \sqrt{$a_x +$b_y}} - \frac{$fc2 y}{\sqrt{$c_xy}}}\) $BR

$BR

Pull the common factor of \(\displaystyle{\frac{dy}{dx}}\): $BR

$BR

\(\displaystyle{  \frac{dy}{dx} \left( \frac{$fb2}{ \sqrt{$a_x +$b_y}} + \frac{$fc2 x}{\sqrt{$c_xy}} \right)  = -\frac{$fa2}{ \sqrt{$a_x +$b_y}} - \frac{$fc2 y}{\sqrt{$c_xy}}}\) $BR

$BR

Isolate  \(\displaystyle{\frac{dy}{dx}}\): $BR

$BR

In reply to tim Payer

Re: Error traced to gcd( ) using more than 2 arguments?

by tim Payer -
NEVER Mind,

Okay Tim Lauck saves the day again.

The trick is to nest the gcd commands to handle say four arguments

Like this:

$gc3 = gcd(gcd(gcd($fa3,$fc3),$fb3),$fd3);

Tim
In reply to tim Payer

Re: Error traced to gcd( ) using more than 2 arguments?

by Alex Jordan -
If I recall, the gcd() that you have access to assumes two inputs, and it only returns the gcd of the first two arguments.

Maybe there is already a simpler way to handle this, but I think in the past I just wrote a loop. If @numbers = (14,14,7,14), then make something like GCD() be a recursive subroutine. Haven't tested, but maybe like:

sub GCD {
my $a = shift;
my $b = shift;
my @leftovers = @_;
my $g = gcd($a,$b);
return (@leftovers == 0) ? $g : GCD($g,@leftovers);
}

While I may not be providing valid perl code, the last line is meant to read "if @leftovers is empty, then return $g; otherwise, return GCD($g,@leftovers)."