3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Returns the total number of $count-length strings generatable from $letters. function getPermCount($letters, $count) { $result = 1; // k characters from a set of n has n!/(n-k)! possible combinations for($i = strlen($letters) - $count + 1; $i <= strlen($letters); $i++) { $result *= $i; } return $result; } // Decodes $index to a $count-length string from $letters, no repeat chars. function getPerm($letters, $count, $index) { $result = array(); for($i = 0; $i < $count; $i++) { $pos = $index % strlen($letters); $result[] = $letters[$pos]; $index = ($index-$pos)/strlen($letters); $letters = substr($letters, 0, $pos) . substr($letters, $pos+1); } sort($result); return implode("", $result); } $letters = 'abc'; $r = array(); //echo '1 letters from 4: '; for($i = 0; $i < getPermCount($letters, 1); $i++) $r[] = getPerm($letters, 1, $i); //echo '2 letters from 4: '; for($i = 0; $i < getPermCount($letters, 2); $i++) $r[] = getPerm($letters, 2, $i); //echo ' 3 letters from 4: '; for($i = 0; $i < getPermCount($letters, 3); $i++) $r[] = getPerm($letters, 3, $i); print_r(array_unique($r, SORT_REGULAR));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 4
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 20
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 36
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 36
Branch analysis from position: 50
Branch analysis from position: 36
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 20
Branch analysis from position: 34
Branch analysis from position: 20
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 4
Branch analysis from position: 18
Branch analysis from position: 4
filename:       /in/NfSia
function name:  (null)
number of ops:  58
compiled vars:  !0 = $letters, !1 = $r, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   ASSIGN                                                   !0, 'abc'
   29     1        ASSIGN                                                   !1, <array>
   32     2        ASSIGN                                                   !2, 0
          3      > JMP                                                      ->12
   33     4    >   INIT_FCALL                                               'getperm'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 1
          7        SEND_VAR                                                 !2
          8        DO_FCALL                                      0  $7      
          9        ASSIGN_DIM                                               !1
         10        OP_DATA                                                  $7
   32    11        PRE_INC                                                  !2
         12    >   INIT_FCALL                                               'getpermcount'
         13        SEND_VAR                                                 !0
         14        SEND_VAL                                                 1
         15        DO_FCALL                                      0  $9      
         16        IS_SMALLER                                               !2, $9
         17      > JMPNZ                                                    ~10, ->4
   36    18    >   ASSIGN                                                   !2, 0
         19      > JMP                                                      ->28
   37    20    >   INIT_FCALL                                               'getperm'
         21        SEND_VAR                                                 !0
         22        SEND_VAL                                                 2
         23        SEND_VAR                                                 !2
         24        DO_FCALL                                      0  $13     
         25        ASSIGN_DIM                                               !1
         26        OP_DATA                                                  $13
   36    27        PRE_INC                                                  !2
         28    >   INIT_FCALL                                               'getpermcount'
         29        SEND_VAR                                                 !0
         30        SEND_VAL                                                 2
         31        DO_FCALL                                      0  $15     
         32        IS_SMALLER                                               !2, $15
         33      > JMPNZ                                                    ~16, ->20
   40    34    >   ASSIGN                                                   !2, 0
         35      > JMP                                                      ->44
   41    36    >   INIT_FCALL                                               'getperm'
         37        SEND_VAR                                                 !0
         38        SEND_VAL                                                 3
         39        SEND_VAR                                                 !2
         40        DO_FCALL                                      0  $19     
         41        ASSIGN_DIM                                               !1
         42        OP_DATA                                                  $19
   40    43        PRE_INC                                                  !2
         44    >   INIT_FCALL                                               'getpermcount'
         45        SEND_VAR                                                 !0
         46        SEND_VAL                                                 3
         47        DO_FCALL                                      0  $21     
         48        IS_SMALLER                                               !2, $21
         49      > JMPNZ                                                    ~22, ->36
   43    50    >   INIT_FCALL                                               'print_r'
         51        INIT_FCALL                                               'array_unique'
         52        SEND_VAR                                                 !1
         53        SEND_VAL                                                 0
         54        DO_ICALL                                         $23     
         55        SEND_VAR                                                 $23
         56        DO_ICALL                                                 
         57      > RETURN                                                   1

Function getpermcount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 8
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 8
Branch analysis from position: 13
Branch analysis from position: 8
filename:       /in/NfSia
function name:  getPermCount
number of ops:  15
compiled vars:  !0 = $letters, !1 = $count, !2 = $result, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        ASSIGN                                                   !2, 1
    7     3        STRLEN                                           ~5      !0
          4        SUB                                              ~6      ~5, !1
          5        ADD                                              ~7      ~6, 1
          6        ASSIGN                                                   !3, ~7
          7      > JMP                                                      ->10
    8     8    >   ASSIGN_OP                                     3          !2, !3
    7     9        PRE_INC                                                  !3
         10    >   STRLEN                                           ~11     !0
         11        IS_SMALLER_OR_EQUAL                                      !3, ~11
         12      > JMPNZ                                                    ~12, ->8
   10    13    > > RETURN                                                   !2
   11    14*     > RETURN                                                   null

End of function getpermcount

Function getperm:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 6
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 6
Branch analysis from position: 31
Branch analysis from position: 6
filename:       /in/NfSia
function name:  getPerm
number of ops:  40
compiled vars:  !0 = $letters, !1 = $count, !2 = $index, !3 = $result, !4 = $i, !5 = $pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   16     3        ASSIGN                                                   !3, <array>
   17     4        ASSIGN                                                   !4, 0
          5      > JMP                                                      ->29
   19     6    >   STRLEN                                           ~8      !0
          7        MOD                                              ~9      !2, ~8
          8        ASSIGN                                                   !5, ~9
   20     9        FETCH_DIM_R                                      ~12     !0, !5
         10        ASSIGN_DIM                                               !3
         11        OP_DATA                                                  ~12
   21    12        SUB                                              ~13     !2, !5
         13        STRLEN                                           ~14     !0
         14        DIV                                              ~15     ~13, ~14
         15        ASSIGN                                                   !2, ~15
   22    16        INIT_FCALL                                               'substr'
         17        SEND_VAR                                                 !0
         18        SEND_VAL                                                 0
         19        SEND_VAR                                                 !5
         20        DO_ICALL                                         $17     
         21        INIT_FCALL                                               'substr'
         22        SEND_VAR                                                 !0
         23        ADD                                              ~18     !5, 1
         24        SEND_VAL                                                 ~18
         25        DO_ICALL                                         $19     
         26        CONCAT                                           ~20     $17, $19
         27        ASSIGN                                                   !0, ~20
   17    28        PRE_INC                                                  !4
         29    >   IS_SMALLER                                               !4, !1
         30      > JMPNZ                                                    ~23, ->6
   24    31    >   INIT_FCALL                                               'sort'
         32        SEND_REF                                                 !3
         33        DO_ICALL                                                 
   25    34        INIT_FCALL                                               'implode'
         35        SEND_VAL                                                 ''
         36        SEND_VAR                                                 !3
         37        DO_ICALL                                         $25     
         38      > RETURN                                                   $25
   26    39*     > RETURN                                                   null

End of function getperm

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.68 ms | 1407 KiB | 29 Q