3v4l.org

run code in 300+ PHP versions simultaneously
<?php function optimalChange(int $amount, array $denominations): ?array { if (!$amount) { return []; // happy ending } foreach ($denominations as $d) { if ($d <= $amount) { $deeper = optimalChange($amount - $d, $denominations); if ($deeper !== null) { $result = array_merge([$d], $deeper); break; // run up the recursive branch } } } return $result ?? null; } $target = 23; $denominations = [10, 5, 2]; // must be rsort()ed at declaration foreach (range(0, 35) as $target) { printf( "%s: %s\n", $target, json_encode( optimalChange($target, $denominations) ?: 'Sorry' ) ); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 23
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 23
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/tgFEi
function name:  (null)
number of ops:  25
compiled vars:  !0 = $target, !1 = $denominations
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   ASSIGN                                                   !0, 23
   21     1        ASSIGN                                                   !1, <array>
   22     2        INIT_FCALL                                               'range'
          3        SEND_VAL                                                 0
          4        SEND_VAL                                                 35
          5        DO_ICALL                                         $4      
          6      > FE_RESET_R                                       $5      $4, ->23
          7    > > FE_FETCH_R                                               $5, !0, ->23
   23     8    >   INIT_FCALL                                               'printf'
   24     9        SEND_VAL                                                 '%25s%3A+%25s%0A'
   25    10        SEND_VAR                                                 !0
   26    11        INIT_FCALL                                               'json_encode'
   27    12        INIT_FCALL                                               'optimalchange'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !1
         15        DO_FCALL                                      0  $6      
         16        JMP_SET                                          ~7      $6, ->18
   28    17        QM_ASSIGN                                        ~7      'Sorry'
         18        SEND_VAL                                                 ~7
   26    19        DO_ICALL                                         $8      
   28    20        SEND_VAR                                                 $8
   23    21        DO_ICALL                                                 
   22    22      > JMP                                                      ->7
         23    >   FE_FREE                                                  $5
   31    24      > RETURN                                                   1

Function optimalchange:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 25
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 25
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 24
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 24
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 24
Branch analysis from position: 25
Branch analysis from position: 25
filename:       /in/tgFEi
function name:  optimalChange
number of ops:  32
compiled vars:  !0 = $amount, !1 = $denominations, !2 = $d, !3 = $deeper, !4 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        BOOL_NOT                                         ~5      !0
          3      > JMPZ                                                     ~5, ->5
    5     4    > > RETURN                                                   <array>
    8     5    > > FE_RESET_R                                       $6      !1, ->25
          6    > > FE_FETCH_R                                               $6, !2, ->25
    9     7    >   IS_SMALLER_OR_EQUAL                                      !2, !0
          8      > JMPZ                                                     ~7, ->24
   10     9    >   INIT_FCALL_BY_NAME                                       'optimalChange'
         10        SUB                                              ~8      !0, !2
         11        SEND_VAL_EX                                              ~8
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0  $9      
         14        ASSIGN                                                   !3, $9
   11    15        TYPE_CHECK                                  1020          !3
         16      > JMPZ                                                     ~11, ->24
   12    17    >   INIT_FCALL                                               'array_merge'
         18        INIT_ARRAY                                       ~12     !2
         19        SEND_VAL                                                 ~12
         20        SEND_VAR                                                 !3
         21        DO_ICALL                                         $13     
         22        ASSIGN                                                   !4, $13
   13    23      > JMP                                                      ->25
    8    24    > > JMP                                                      ->6
         25    >   FE_FREE                                                  $6
   17    26        COALESCE                                         ~15     !4
         27        QM_ASSIGN                                        ~15     null
         28        VERIFY_RETURN_TYPE                                       ~15
         29      > RETURN                                                   ~15
   18    30*       VERIFY_RETURN_TYPE                                       
         31*     > RETURN                                                   null

End of function optimalchange

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.94 ms | 1010 KiB | 18 Q