Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\n"; // build list of units (denominators are keys, values are arrays of nominators) $units = array(1 => array(1),); // keep map to avoid duplicates. keys are rounded to thousands (avoid float issues) $percentages = array( '100' => array(1, 1), ); for ($den = 2; $den <= 6; $den++) { for ($num = 1; $num < $den; $num++) { // avoid duplicates $rounded_percentage = (string)round($num / $den, 3); if ($num > 1 && isset($percentages[$rounded_percentage])) { continue; } $percentages[$rounded_percentage] = array($num, $den); $units[$den][] = $num; } } // build rows $rows = array(); $total = 0; for ($den = 1; $den <= count($units); $den++) { // may take multiple rows to use up available units while ($units[$den]) { $row = array(); $nom = array_shift($units[$den]); $row[] = "$nom/$den"; $total += $nom; if ($total < $den) { $nom = $den - $total; $row[] = "$nom/$den"; $total += $nom; $index = array_search($nom, $units[$den]); if ($index !== false) { unset($units[$den][$index]); } } $rows[] = $row; $total = 0; } } ?>