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"; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dQmdb
function name:  (null)
number of ops:  13
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL_BY_NAME                                       'test'
          1        SEND_VAL_EX                                              2000
          2        DO_FCALL                                      0          
    4     3        INIT_FCALL_BY_NAME                                       'test'
          4        SEND_VAL_EX                                              1845
          5        DO_FCALL                                      0          
    5     6        INIT_FCALL_BY_NAME                                       'test'
          7        SEND_VAL_EX                                              2340
          8        DO_FCALL                                      0          
    6     9        INIT_FCALL_BY_NAME                                       'test'
         10        SEND_VAL_EX                                              3000
         11        DO_FCALL                                      0          
   49    12      > RETURN                                                   1

Function calcoptimalpanels:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 16
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 18
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 16
Branch analysis from position: 47
Branch analysis from position: 16
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 31
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 34
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 18
Branch analysis from position: 44
Branch analysis from position: 18
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 18
Branch analysis from position: 44
Branch analysis from position: 18
filename:       /in/dQmdb
function name:  calcOptimalPanels
number of ops:  57
compiled vars:  !0 = $doorHeight, !1 = $bigHeight, !2 = $smallHeight, !3 = $bigFit, !4 = $smallFit, !5 = $options, !6 = $big, !7 = $small, !8 = $waste, !9 = $minWaste
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   15     1        ASSIGN                                                   !1, 615
   16     2        ASSIGN                                                   !2, 495
   18     3        INIT_FCALL                                               'floor'
          4        DIV                                              ~12     !0, !1
          5        SEND_VAL                                                 ~12
          6        DO_ICALL                                         $13     
          7        ASSIGN                                                   !3, $13
   19     8        INIT_FCALL                                               'floor'
          9        DIV                                              ~15     !0, !2
         10        SEND_VAL                                                 ~15
         11        DO_ICALL                                         $16     
         12        ASSIGN                                                   !4, $16
   21    13        ASSIGN                                                   !5, <array>
   23    14        ASSIGN                                                   !6, 0
         15      > JMP                                                      ->45
   24    16    >   ASSIGN                                                   !7, 0
         17      > JMP                                                      ->42
   25    18    >   MUL                                              ~21     !1, !6
         19        MUL                                              ~22     !2, !7
         20        ADD                                              ~23     ~21, ~22
         21        SUB                                              ~24     ~23, !0
         22        ASSIGN                                                   !8, ~24
   27    23        IS_IDENTICAL                                             !8, 0
         24      > JMPZ                                                     ~26, ->31
   28    25    >   INIT_FCALL_BY_NAME                                       'getFormattedResult'
         26        SEND_VAR_EX                                              !6
         27        SEND_VAR_EX                                              !7
         28        SEND_VAR_EX                                              !8
         29        DO_FCALL                                      0  $27     
         30      > RETURN                                                   $27
   30    31    >   IS_SMALLER                                               !8, 0
         32      > JMPZ                                                     ~28, ->34
   31    33    > > JMP                                                      ->41
   33    34    >   INIT_FCALL_BY_NAME                                       'getFormattedResult'
         35        SEND_VAR_EX                                              !6
         36        SEND_VAR_EX                                              !7
         37        SEND_VAR_EX                                              !8
         38        DO_FCALL                                      0  $30     
         39        ASSIGN_DIM                                               !5, !8
         40        OP_DATA                                                  $30
   24    41    >   PRE_INC                                                  !7
         42    >   IS_SMALLER_OR_EQUAL                                      !7, !4
         43      > JMPNZ                                                    ~32, ->18
   23    44    >   PRE_INC                                                  !6
         45    >   IS_SMALLER_OR_EQUAL                                      !6, !3
         46      > JMPNZ                                                    ~34, ->16
   37    47    >   INIT_FCALL                                               'min'
         48        INIT_FCALL                                               'array_keys'
         49        SEND_VAR                                                 !5
         50        DO_ICALL                                         $35     
         51        SEND_VAR                                                 $35
         52        DO_ICALL                                         $36     
         53        ASSIGN                                                   !9, $36
   39    54        FETCH_DIM_R                                      ~38     !5, !9
         55      > RETURN                                                   ~38
   40    56*     > RETURN                                                   null

End of function calcoptimalpanels

Function getformattedresult:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dQmdb
function name:  getFormattedResult
number of ops:  8
compiled vars:  !0 = $big, !1 = $small, !2 = $waste
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   43     3        INIT_ARRAY                                       ~3      !0, '615mm'
          4        ADD_ARRAY_ELEMENT                                ~3      !1, '495mm'
          5        ADD_ARRAY_ELEMENT                                ~3      !2, 'waste'
          6      > RETURN                                                   ~3
   44     7*     > RETURN                                                   null

End of function getformattedresult

Function test:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dQmdb
function name:  test
number of ops:  12
compiled vars:  !0 = $doorHeight
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
   47     1        CONCAT                                           ~1      !0, '%3A+'
          2        INIT_FCALL                                               'json_encode'
          3        INIT_FCALL                                               'calcoptimalpanels'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                         $3      
          8        CONCAT                                           ~4      ~1, $3
          9        CONCAT                                           ~5      ~4, '%0A'
         10        ECHO                                                     ~5
   48    11      > RETURN                                                   null

End of function test

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.71 ms | 1012 KiB | 18 Q