### Re: Reducing square roots using if, ifelse nested within a do loop

by Alex Jordan -https://github.com/openwebwork/pg/blob/master/macros/contextLimitedRadical.pl

### Re: Reducing square roots using if, ifelse nested within a do loop

by Alex Jordan -### Re: Reducing square roots using if, ifelse nested within a do loop

by Davide Cervone -`elsif`

not `elseif`

(for no good reason that I know of). Second, an `elsif`

must follow its corresponding `if`

directly (with no intervening commands). You have inserted several assignments between yours.
Your loop might be handled as follows:

for ($i = floor(sqrt($rad)); $i > 1; $i--) { $r = $rad / ($i*$i); last if $r == int($r); } $crt = ($i == 1 ? "sqrt($rad)" : "$i*sqrt($r)");

A couple of other things to note: your assignment `$crt = "\sqrt($rad)";`

seems strange to me, since it is neither TeX nor algebra format (did you mean `$crt = "\sqrt{$rad}";`

or `$crt = "sqrt($rad)";`

instead?), and since you never actually *use* `$crt`

, I'm not sure why you need it anyway.

Note that

SOLUTION(EV3(<<'END_SOLUTION')); $PAR SOLUTION $PARcan be replaced by

BEGIN_SOLUTION

Finally, you seem to use a *lot* of variables for intermediate values that complicated the problem in my view. There are two reasons to store something in a variable: you need to use that value several times, or you need to insert the result of a computation into a `Compute()`

string, or the text of the problem (or solution or hint, etc). But it really isn't necessary to use a variable for the second of these, as there are ways to do the computations and have them inserted into the `Compute()`

string or within `BEGIN_TEXT/END_TEXT`

or in `PGML`

.

For example, you can do

$x = Compute("sqrt(".($a*$b).")");if you want to perform the computation

`$a*$b`

and have that inside the square root in the correct answer (if you did `compute("sqrt($a*$b)")`

you would see something like `sqrt(4*7)`

as the correct answer).
For problem text, you can use `\{...\}`

around a computation to get its result inserted into the text:

BEGIN_TEXT The value of \($a + $b\) is \(\{$a+$b\}\). END_TEXTThere is no need to make a variable holding

`$a+$b`

and substitute that.
Similarly, in PGML you can do

BEGIN_PGML The value of [`[$a] + [$b]`] is [`[$a+$b]`]. END_PGMLor

BEGIN_PGML The value of [`[$a] + [$b]`] is [`[@ $a+$b @]`]. END_PGMLSo there really is no need to make dozens of variable holding intermediate computations that just clutter up the flow of the problem.

### Re: Reducing square roots using if, ifelse nested within a do loop

by tim Payer -### Re: Reducing square roots using if, ifelse nested within a do loop

by Davide Cervone -`$i == 1`

rather than `$i = 1`

. The former tests if `$i`

is 1 or not, and the latter assigns the value 1 to `$i`

(and returns the value 1, so is always considered true, so the loop will stop).But if you use the loop I suggested, that replaces the do loop anyway.