' . getFractionOperationDisplayTable($b[$pos1 - 1], $o[$op], $b[$pos2 - 1]) . ''; } function getFractionOperationDisplayTable($fraction1, $operation, $fraction2) { $t = ''; $t .= ''; $t .= '' . ''; $t .= '' . ''; $t .= '
' . $fraction1[0] . '' . $operation . '' . $fraction2[0] . '
' .$fraction1[1] . '' .$fraction2[1] . '
'; return $t; } function getFractionDisplayTable($fraction) { if ($fraction[0] == 0) { $t = '0'; } elseif ($fraction[0] == $fraction[1]) { $t = '1'; } elseif ($fraction[1] == 1) { $t = $fraction[0]; } else { $t = ''; $t .= ''; if ($fraction[0] > 0) { $t .= ''; } else { $t .= '' . '' . ''; } $t .= ''; $t .= '
' . $fraction[0] . '
' . (-1 * $fraction[0]) . '
' . $fraction[1] . '
'; } return $t; } function shortenFraction(&$fraction) { $lessi = abs($fraction[0]); if ($fraction[1] < $lessi) $lessi = abs($fraction[1]); for ($c=2; $c<=$lessi; $c++) { while (($c * floor($fraction[0]/$c) == $fraction[0]) && ($c * floor($fraction[1]/$c) == $fraction[1])) { $fraction[0] /= $c; $fraction[1] /= $c; } } } function solution($number) { $b = Array ( Array(1,2), Array(1,3), Array(1,4), Array(2,3), Array(3,2), Array(3,4), Array(3,4), Array(3,2), Array(2,3), Array(1,4), Array(1,3), Array(1,2) ); $o = Array ( '+', '−', '·', ':' ); $op = ($number - 1) % 4; $numb = floor(($number - 1) / 4) + 1; $block = floor(sqrt(2*$numb - 7/4)-1/2); $start = 0.5 * $block * $block + 0.5 * $block + 1; $pos1 = $numb - $start + 1; $pos2 = $block + 2 - $pos1; $b1 = $b[$pos1 - 1]; $b2 = $b[$pos2 - 1]; switch ($op) { case 0: if ($b1[1] != $b2[1]) { $e1 = $b2[1]; $e2 = $b1[1]; $b1[0] *= $e1; $b1[1] *= $e1; $b2[0] *= $e2; $b2[1] *= $e2; } $r = Array($b1[0] + $b2[0], $b1[1]); shortenFraction($r); break; case 1: if ($b1[1] != $b2[1]) { $e1 = $b2[1]; $e2 = $b1[1]; $b1[0] *= $e1; $b1[1] *= $e1; $b2[0] *= $e2; $b2[1] *= $e2; } $r = Array($b1[0] - $b2[0], $b1[1]); shortenFraction($r); break; case 2: $r = Array($b1[0] * $b2[0], $b1[1] * $b2[1]); shortenFraction($r); break; case 3: $r = Array($b1[0] * $b2[1], $b1[1] * $b2[0]); shortenFraction($r); break; } return '
' . getFractionDisplayTable($r) . '
'; } ?>