3v4l.org

run code in 300+ 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 = 20
Branch analysis from position: 1
2 jumps found. (Code = 78) Position 1 = 2, Position 2 = 20
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
Found catch point at position: 14
Branch analysis from position: 14
2 jumps found. (Code = 107) Position 1 = 15, Position 2 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
filename:       /in/L7ft1
function name:  (null)
number of ops:  22
compiled vars:  !0 = $test, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E > > FE_RESET_R                                       $2      <array>, ->20
          1    > > FE_FETCH_R                                               $2, !0, ->20
   32     2    >   ECHO                                                     !0
          3        ECHO                                                     '+%3D%3E+'
          4        INIT_FCALL                                               'implode'
          5        SEND_VAL                                                 '%2C+'
          6        INIT_FCALL                                               'getcoins'
          7        SEND_VAR                                                 !0
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR                                                 $3
         10        DO_ICALL                                         $4      
         11        ECHO                                                     $4
         12        ECHO                                                     '%0A'
         13      > JMP                                                      ->19
   33    14  E > > CATCH                                       last         'Exception'
   34    15    >   INIT_METHOD_CALL                                         !1, 'getMessage'
         16        DO_FCALL                                      0  $5      
         17        ECHO                                                     $5
         18        ECHO                                                     '%0A'
   30    19    > > JMP                                                      ->1
         20    >   FE_FREE                                                  $2
   36    21      > 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 = 56
Branch analysis from position: 45
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
2 jumps found. (Code = 44) Position 1 = 67, Position 2 = 58
Branch analysis from position: 67
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 67, Position 2 = 58
Branch analysis from position: 67
Branch analysis from position: 58
Branch analysis from position: 41
filename:       /in/L7ft1
function name:  getCoins
number of ops:  69
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, ->56
   18    45    >   NEW                                              $35     'Exception'
         46        NOP                                                      
         47        FAST_CONCAT                                      ~36     !0, '+is+not+possible+with+the+denominations+'
         48        INIT_FCALL                                               'implode'
         49        SEND_VAL                                                 '%2C'
         50        SEND_VAR                                                 !1
         51        DO_ICALL                                         $37     
         52        CONCAT                                           ~38     ~36, $37
         53        SEND_VAL_EX                                              ~38
         54        DO_FCALL                                      0          
         55      > THROW                                         0          $35
   21    56    >   ASSIGN                                                   !5, <array>
   22    57      > JMP                                                      ->65
   23    58    >   FETCH_DIM_R                                      ~42     !2, !0
         59        FETCH_DIM_R                                      ~43     ~42, 1
         60        ASSIGN_DIM                                               !5
         61        OP_DATA                                                  ~43
   24    62        FETCH_DIM_R                                      ~44     !2, !0
         63        FETCH_DIM_R                                      ~45     ~44, 1
         64        ASSIGN_OP                                     2          !0, ~45
   22    65    >   IS_SMALLER                                               0, !0
         66      > JMPNZ                                                    ~47, ->58
   27    67    > > RETURN                                                   !5
   28    68*     > RETURN                                                   null

End of function getcoins

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.4 ms | 1011 KiB | 15 Q