php - Do-While Loop with Multiple Conditions -
can do-while loop have multiple conditions? if so, can't figure out why code below failing on first condition.
functions used...
function gcf($a,$b) { $a = abs($a); $b = abs($b); if( $a < $b) list($b,$a) = array($a,$b); if( $b == 0) return $a; $r = $a % $b; while($r > 0) { $a = $b; $b = $r; $r = $a % $b; } return $b; } function factors($n){ $factors_array = array(); ($x = 1; $x <= sqrt(abs($n)); $x++) { if ($n % $x == 0) { $z = $n/$x; array_push($factors_array, $x, $z); } } return $factors_array; }
code...
$a = $b; do{ $a = mt_rand(8, 100); $a_factors_array = factors($a); $b = mt_rand(8, 100); $b_factors_array = factors($b); } while ($a == $b && count($a_factors_array) < 4 && count($b_factors_array) < 4 && gcf($a, $b) == 1); echo $a . '<br>'; echo $b . '<br>'; echo count($a_factors_array) . '<br>'; echo count($b_factors_array) . '<br>'; echo gcf($a, $b) . '<br>';
i keep getting numbers $a , $b have less 4 factors , have gcf of 1. ideas?
you'll need ||
instead of &&
. want repeat loop long any 1 of conditions met. loop repeated if all of conditions met.
Comments
Post a Comment