3v4l.org

run code in 500+ PHP versions simultaneously
<?php function getCoins(int $totalAmount, $denominations = [2 ,5, 10]){ $amtPossible = []; foreach($denominations as $d){ $amtPossible[ $d ]= [1, $d]; for($i = $d + 1; $i <= $totalAmount; ++$i){ if(isset($amtPossible[ $i - $d ])){ if(!isset($amtPossible[ $i ]) || $amtPossible[ $i ][0] > 1 + $amtPossible[ $i - $d ][0]){ $amtPossible[ $i ][0] = 1 + $amtPossible[ $i - $d ][0]; $amtPossible[ $i ][1] = $d; } } } } if(!isset($amtPossible[ $totalAmount ])){ throw new \Exception("$totalAmount is not possible with the denominations ". implode(",", $denominations)); } $coins = []; while($totalAmount > 0){ $coins[] = $amtPossible[ $totalAmount ][1]; $totalAmount -= $amtPossible[ $totalAmount ][1]; } return $coins; } foreach([10, 11, 21, 23, 31, 3] as $test){ try{ echo $test, " => ", implode(", ", getCoins($test)), PHP_EOL; }catch(\Exception $e){ echo $e->getMessage(), PHP_EOL; } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 1, Position 2 = 17
Branch analysis from position: 1
2 jumps found. (Code = 78) Position 1 = 2, Position 2 = 17
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
Found catch point at position: 11
Branch analysis from position: 11
2 jumps found. (Code = 107) Position 1 = 12, Position 2 = -2
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
filename:       /in/L7ft1
function name:  (null)
number of ops:  19
compiled vars:  !0 = $test, !1 = $e
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   30     0  E > > FE_RESET_R                                           $2      <array>, ->17
          1    > > FE_FETCH_R                                                   $2, !0, ->17
   32     2    >   ECHO                                                         !0
          3        ECHO                                                         '+%3D%3E+'
          4        INIT_FCALL                                                   'getcoins'
          5        SEND_VAR                                                     !0
          6        DO_FCALL                                          0  $3      
          7        FRAMELESS_ICALL_2                implode             ~4      '%2C+', $3
          8        ECHO                                                         ~4
          9        ECHO                                                         '%0A'
         10      > JMP                                                          ->16
   33    11  E > > CATCH                                           last         'Exception'
   34    12    >   INIT_METHOD_CALL                                             !1, 'getMessage'
         13        DO_FCALL                                          0  $5      
         14        ECHO                                                         $5
         15        ECHO                                                         '%0A'
   30    16    > > JMP                                                          ->1
         17    >   FE_FREE                                                      $2
   36    18      > RETURN                                                       1

Function getcoins:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 41
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 41
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 12
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 37
Branch analysis from position: 15
2 jumps found. (Code = 47) Position 1 = 18, Position 2 = 26
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 37
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 40, Position 2 = 12
Branch analysis from position: 40
Branch analysis from position: 12
Branch analysis from position: 37
Branch analysis from position: 26
Branch analysis from position: 37
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 53
Branch analysis from position: 45
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 55
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 55
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 55
Branch analysis from position: 64
Branch analysis from position: 55
Branch analysis from position: 41
filename:       /in/L7ft1
function name:  getCoins
number of ops:  66
compiled vars:  !0 = $totalAmount, !1 = $denominations, !2 = $amtPossible, !3 = $d, !4 = $i, !5 = $coins
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      <array>
    4     2        ASSIGN                                                       !2, <array>
    5     3      > FE_RESET_R                                           $7      !1, ->41
          4    > > FE_FETCH_R                                                   $7, !3, ->41
    6     5    >   INIT_ARRAY                                           ~9      1
          6        ADD_ARRAY_ELEMENT                                    ~9      !3
          7        ASSIGN_DIM                                                   !2, !3
          8        OP_DATA                                                      ~9
    7     9        ADD                                                  ~10     !3, 1
         10        ASSIGN                                                       !4, ~10
         11      > JMP                                                          ->38
    8    12    >   SUB                                                  ~12     !4, !3
         13        ISSET_ISEMPTY_DIM_OBJ                             0          !2, ~12
         14      > JMPZ                                                         ~13, ->37
    9    15    >   ISSET_ISEMPTY_DIM_OBJ                             0  ~14     !2, !4
         16        BOOL_NOT                                             ~15     ~14
         17      > JMPNZ_EX                                             ~15     ~15, ->26
         18    >   FETCH_DIM_R                                          ~16     !2, !4
         19        FETCH_DIM_R                                          ~17     ~16, 0
         20        SUB                                                  ~18     !4, !3
         21        FETCH_DIM_R                                          ~19     !2, ~18
         22        FETCH_DIM_R                                          ~20     ~19, 0
         23        ADD                                                  ~21     1, ~20
         24        IS_SMALLER                                           ~22     ~21, ~17
         25        BOOL                                                 ~15     ~22
         26    > > JMPZ                                                         ~15, ->37
   10    27    >   SUB                                                  ~25     !4, !3
         28        FETCH_DIM_R                                          ~26     !2, ~25
         29        FETCH_DIM_R                                          ~27     ~26, 0
         30        ADD                                                  ~28     1, ~27
         31        FETCH_DIM_W                                          $23     !2, !4
         32        ASSIGN_DIM                                                   $23, 0
         33        OP_DATA                                                      ~28
   11    34        FETCH_DIM_W                                          $29     !2, !4
         35        ASSIGN_DIM                                                   $29, 1
         36        OP_DATA                                                      !3
    7    37    >   PRE_INC                                                      !4
         38    >   IS_SMALLER_OR_EQUAL                                          !4, !0
         39      > JMPNZ                                                        ~32, ->12
    5    40    > > JMP                                                          ->4
         41    >   FE_FREE                                                      $7
   17    42        ISSET_ISEMPTY_DIM_OBJ                             0  ~33     !2, !0
         43        BOOL_NOT                                             ~34     ~33
         44      > JMPZ                                                         ~34, ->53
   18    45    >   NEW                                                  $35     'Exception'
         46        NOP                                                          
         47        FAST_CONCAT                                          ~36     !0, '+is+not+possible+with+the+denominations+'
         48        FRAMELESS_ICALL_2                implode             ~37     '%2C', !1
         49        CONCAT                                               ~38     ~36, ~37
         50        SEND_VAL_EX                                                  ~38
         51        DO_FCALL                                          0          
         52      > THROW                                             0          $35
   21    53    >   ASSIGN                                                       !5, <array>
   22    54      > JMP                                                          ->62
   23    55    >   FETCH_DIM_R                                          ~42     !2, !0
         56        FETCH_DIM_R                                          ~43     ~42, 1
         57        ASSIGN_DIM                                                   !5
         58        OP_DATA                                                      ~43
   24    59        FETCH_DIM_R                                          ~44     !2, !0
         60        FETCH_DIM_R                                          ~45     ~44, 1
         61        ASSIGN_OP                                         2          !0, ~45
   22    62    >   IS_SMALLER                                                   0, !0
         63      > JMPNZ                                                        ~47, ->55
   27    64    > > RETURN                                                       !5
   28    65*     > RETURN                                                       null

End of function getcoins

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
159.87 ms | 1614 KiB | 16 Q