3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Sums { public function getAllSums($total) { if ($total < 0) { return array(); } $return = array(); // Reducing the problem linearly, examining one integer at a time for ($i = $total; $i >= 1; $i--) { $subTotal = $this->getSumsFor($i, $total); foreach ($subTotal as $item) { $return[] = $item; } } return $return; } private function getSumsFor($currentValue, $total) { $return = array(); // Base case where the current iteration is its own sum if ($currentValue == $total) { $return[] = array($total); return $return; } // Base case where we have all the 1's summed up else if ($currentValue == 1) { $sum = array(); for ($i = 0; $i < $total; $i++) { $sum[] = 1; } $return[] = $sum; return $return; } $remainder = $total - $currentValue; // Break the problem into a smaller chunk $possibleSums = $this->getAllSums($remainder); foreach ($possibleSums as $row) { // Once we have the sum of the remainder, add the original value back array_unshift($row, $currentValue); $return[] = $row; } return $return; } } $sums = new Sums(); echo '<pre>'; print_r($sums->getAllSums(4)); echo '</pre>';
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SXnXt
function name:  (null)
number of ops:  12
compiled vars:  !0 = $sums
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   NEW                                              $1      'Sums'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   46     3        ECHO                                                     '%3Cpre%3E'
   47     4        INIT_FCALL                                               'print_r'
          5        INIT_METHOD_CALL                                         !0, 'getAllSums'
          6        SEND_VAL_EX                                              4
          7        DO_FCALL                                      0  $4      
          8        SEND_VAR                                                 $4
          9        DO_ICALL                                                 
   48    10        ECHO                                                     '%3C%2Fpre%3E'
         11      > RETURN                                                   1

Class Sums:
Function getallsums:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 7
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 7
Branch analysis from position: 21
Branch analysis from position: 7
Branch analysis from position: 17
filename:       /in/SXnXt
function name:  getAllSums
number of ops:  23
compiled vars:  !0 = $total, !1 = $return, !2 = $i, !3 = $subTotal, !4 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        IS_SMALLER                                               !0, 0
          2      > JMPZ                                                     ~5, ->4
    5     3    > > RETURN                                                   <array>
    7     4    >   ASSIGN                                                   !1, <array>
    9     5        ASSIGN                                                   !2, !0
          6      > JMP                                                      ->19
   10     7    >   INIT_METHOD_CALL                                         'getSumsFor'
          8        SEND_VAR_EX                                              !2
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0  $8      
         11        ASSIGN                                                   !3, $8
   11    12      > FE_RESET_R                                       $10     !3, ->17
         13    > > FE_FETCH_R                                               $10, !4, ->17
   12    14    >   ASSIGN_DIM                                               !1
         15        OP_DATA                                                  !4
   11    16      > JMP                                                      ->13
         17    >   FE_FREE                                                  $10
    9    18        PRE_DEC                                                  !2
         19    >   IS_SMALLER_OR_EQUAL                                      1, !2
         20      > JMPNZ                                                    ~13, ->7
   15    21    > > RETURN                                                   !1
   16    22*     > RETURN                                                   null

End of function getallsums

Function getsumsfor:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 23
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 15
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 15
Branch analysis from position: 20
Branch analysis from position: 15
Branch analysis from position: 23
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 38
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 38
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
filename:       /in/SXnXt
function name:  getSumsFor
number of ops:  41
compiled vars:  !0 = $currentValue, !1 = $total, !2 = $return, !3 = $sum, !4 = $i, !5 = $remainder, !6 = $possibleSums, !7 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   19     2        ASSIGN                                                   !2, <array>
   21     3        IS_EQUAL                                                 !0, !1
          4      > JMPZ                                                     ~9, ->10
   22     5    >   INIT_ARRAY                                       ~11     !1
          6        ASSIGN_DIM                                               !2
          7        OP_DATA                                                  ~11
   23     8      > RETURN                                                   !2
          9*       JMP                                                      ->23
   26    10    >   IS_EQUAL                                                 !0, 1
         11      > JMPZ                                                     ~12, ->23
   27    12    >   ASSIGN                                                   !3, <array>
   28    13        ASSIGN                                                   !4, 0
         14      > JMP                                                      ->18
   29    15    >   ASSIGN_DIM                                               !3
         16        OP_DATA                                                  1
   28    17        PRE_INC                                                  !4
         18    >   IS_SMALLER                                               !4, !1
         19      > JMPNZ                                                    ~17, ->15
   31    20    >   ASSIGN_DIM                                               !2
         21        OP_DATA                                                  !3
   32    22      > RETURN                                                   !2
   34    23    >   SUB                                              ~19     !1, !0
         24        ASSIGN                                                   !5, ~19
   36    25        INIT_METHOD_CALL                                         'getAllSums'
         26        SEND_VAR_EX                                              !5
         27        DO_FCALL                                      0  $21     
         28        ASSIGN                                                   !6, $21
   37    29      > FE_RESET_R                                       $23     !6, ->38
         30    > > FE_FETCH_R                                               $23, !7, ->38
   39    31    >   INIT_FCALL                                               'array_unshift'
         32        SEND_REF                                                 !7
         33        SEND_VAR                                                 !0
         34        DO_ICALL                                                 
   40    35        ASSIGN_DIM                                               !2
         36        OP_DATA                                                  !7
   37    37      > JMP                                                      ->30
         38    >   FE_FREE                                                  $23
   42    39      > RETURN                                                   !2
   43    40*     > RETURN                                                   null

End of function getsumsfor

End of class Sums.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.65 ms | 1404 KiB | 17 Q