Missing the homework flag?
Think in base 6, and run a loop from zero to 6^3 (216) then use mod to reduce/shift back into x y z
positions.
Question
Rewrite the code below so it would contain only one loop and only one itterational variable would be used.
$result = Array();
for ($x = 0; $x < 6; $x++){
for ($y = 0; $y < 6; $y++){
for ($z = 0; $z < 6; $z++){
$result[$x][$y][$z] = $x * $y * $z;
}
}
}
NO ANSWERS PLEASE, JUST POINT ME IN THE RIGHT DIRECTION. Is there a php class that can handle this? Or is it just a simple problem solving technique? Or is it a trick question?
Recommended result below:
$result = Array();
for ($i = 0; $i < 216; $i++){
$x = $i % 6;
$y = floor($i/6) % 6;
$z = floor($i/36);
$result[$x][$y][$z] = $x * $y * $z;
}
Solution
Missing the homework flag?
Think in base 6, and run a loop from zero to 6^3 (216) then use mod to reduce/shift back into x y z
positions.
OTHER TIPS
Pro Tip #1: You can have multiple variable operations in a single for loop...
for($a = 0, $b = 10; $b > $a; $b++, $a--) {
echo $a . $b;
}