3v4l.org

run code in 300+ PHP versions simultaneously
<?php test(2000); test(1845); test(2340); test(3000); // Examples: // Door 2000 1845 3000 // 615mm panel 1 3 5 // 495mm panel 3 0 0 // panel loss 100 0 75 function calcOptimalPanels ($doorHeight) { $bigHeight = 615; $smallHeight = 495; $bigFit = floor($doorHeight / $bigHeight); $smallFit = floor($doorHeight / $smallHeight); $options = []; for ($big = 0; $big <= $bigFit; $big++) { for ($small = 0; $small <= $smallFit; $small++) { $waste = $bigHeight * $big + $smallHeight * $small - $doorHeight; if ($waste === 0) // Get first combination without waste return getFormattedResult($big, $small, $waste); if ($waste < 0) // Omit combinations smaller then door continue; $options[$waste] = getFormattedResult($big, $small, $waste); } } $minWaste = min(array_keys($options)); return $options[$minWaste]; } function getFormattedResult($big, $small, $waste) { return ['615mm' => $big, '495mm' => $small, 'waste' => $waste]; } function test($doorHeight) { echo $doorHeight . ': ' . json_encode(calcOptimalPanels($doorHeight)) . "\n"; }
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
2000: {"615mm":1,"495mm":3,"waste":100} 1845: {"615mm":3,"495mm":0,"waste":0} 2340: {"615mm":3,"495mm":1,"waste":0} 3000: {"615mm":1,"495mm":5,"waste":90}
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 2000: {"615mm":1,"495mm":3,"waste":100} 1845: {"615mm":3,"495mm":0,"waste":0} 2340: {"615mm":3,"495mm":1,"waste":0} 3000: {"615mm":1,"495mm":5,"waste":90}

preferences:
222.12 ms | 402 KiB | 290 Q