3v4l.org

run code in 300+ PHP versions simultaneously
<?php $digits = array(9,8,7,6,5,4,3,2,1); $primes = array(1,2,3,5,7,11); //will return 1:1 for 5:5. The function does not try to simplify them further function simplify(Int $num, Int $denom, Array $primes){ //going from high primes to low primes foreach(array_reverse($primes) as $prime) { if (($num % $prime == 0) && ($denom % $prime == 0)) { if ($num % $denom == 0) { return [intdiv($num,$denom)]; } elseif ($denom % $num == 0) { return [1,intdiv($denom,$num)]; //return simplify($num,$denom,$primes); } return [$num / $prime,$denom / $prime]; } } return [$num,$denom]; } function simplifyArrays(Array $arr1, Array $arr2, Array $primes) { for($i=0;$i<count($arr1);$i++){ $num1 = $arr1[$i]; for($j=0;$j<count($arr2);$j++){ $num2 = $arr2[$j]; echo "simplify: $num1 / $num2 = " . implode("/",simplify($num1,$num2,$primes)) . "\n"; } } } simplifyArrays($digits,$digits,$primes);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qmgJX
function name:  (null)
number of ops:  8
compiled vars:  !0 = $digits, !1 = $primes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
    3     1        ASSIGN                                                   !1, <array>
   31     2        INIT_FCALL                                               'simplifyarrays'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0          
          7      > RETURN                                                   1

Function simplify:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 44
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 44
Branch analysis from position: 8
2 jumps found. (Code = 46) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 43
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 26
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 37
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 14
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
filename:       /in/qmgJX
function name:  simplify
number of ops:  49
compiled vars:  !0 = $num, !1 = $denom, !2 = $primes, !3 = $prime
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    8     3        INIT_FCALL                                               'array_reverse'
          4        SEND_VAR                                                 !2
          5        DO_ICALL                                         $4      
          6      > FE_RESET_R                                       $5      $4, ->44
          7    > > FE_FETCH_R                                               $5, !3, ->44
    9     8    >   MOD                                              ~6      !0, !3
          9        IS_EQUAL                                         ~7      ~6, 0
         10      > JMPZ_EX                                          ~7      ~7, ->14
         11    >   MOD                                              ~8      !1, !3
         12        IS_EQUAL                                         ~9      ~8, 0
         13        BOOL                                             ~7      ~9
         14    > > JMPZ                                                     ~7, ->43
   10    15    >   MOD                                              ~10     !0, !1
         16        IS_EQUAL                                                 ~10, 0
         17      > JMPZ                                                     ~11, ->26
   11    18    >   INIT_FCALL                                               'intdiv'
         19        SEND_VAR                                                 !0
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                         $12     
         22        INIT_ARRAY                                       ~13     $12
         23        FE_FREE                                                  $5
         24      > RETURN                                                   ~13
         25*       JMP                                                      ->37
   12    26    >   MOD                                              ~14     !1, !0
         27        IS_EQUAL                                                 ~14, 0
         28      > JMPZ                                                     ~15, ->37
   13    29    >   INIT_ARRAY                                       ~16     1
         30        INIT_FCALL                                               'intdiv'
         31        SEND_VAR                                                 !1
         32        SEND_VAR                                                 !0
         33        DO_ICALL                                         $17     
         34        ADD_ARRAY_ELEMENT                                ~16     $17
         35        FE_FREE                                                  $5
         36      > RETURN                                                   ~16
   16    37    >   DIV                                              ~18     !0, !3
         38        INIT_ARRAY                                       ~19     ~18
         39        DIV                                              ~20     !1, !3
         40        ADD_ARRAY_ELEMENT                                ~19     ~20
         41        FE_FREE                                                  $5
         42      > RETURN                                                   ~19
    8    43    > > JMP                                                      ->7
         44    >   FE_FREE                                                  $5
   19    45        INIT_ARRAY                                       ~21     !0
         46        ADD_ARRAY_ELEMENT                                ~21     !1
         47      > RETURN                                                   ~21
   20    48*     > RETURN                                                   null

End of function simplify

Function simplifyarrays:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 5
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 9
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 5
Branch analysis from position: 36
Branch analysis from position: 5
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 9
Branch analysis from position: 32
Branch analysis from position: 9
filename:       /in/qmgJX
function name:  simplifyArrays
number of ops:  37
compiled vars:  !0 = $arr1, !1 = $arr2, !2 = $primes, !3 = $i, !4 = $num1, !5 = $j, !6 = $num2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   22     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->33
   23     5    >   FETCH_DIM_R                                      ~8      !0, !3
          6        ASSIGN                                                   !4, ~8
   24     7        ASSIGN                                                   !5, 0
          8      > JMP                                                      ->29
   25     9    >   FETCH_DIM_R                                      ~11     !1, !5
         10        ASSIGN                                                   !6, ~11
   26    11        ROPE_INIT                                     5  ~14     'simplify%3A+'
         12        ROPE_ADD                                      1  ~14     ~14, !4
         13        ROPE_ADD                                      2  ~14     ~14, '+%2F+'
         14        ROPE_ADD                                      3  ~14     ~14, !6
         15        ROPE_END                                      4  ~13     ~14, '+%3D+'
         16        INIT_FCALL                                               'implode'
         17        SEND_VAL                                                 '%2F'
         18        INIT_FCALL                                               'simplify'
         19        SEND_VAR                                                 !4
         20        SEND_VAR                                                 !6
         21        SEND_VAR                                                 !2
         22        DO_FCALL                                      0  $17     
         23        SEND_VAR                                                 $17
         24        DO_ICALL                                         $18     
         25        CONCAT                                           ~19     ~13, $18
         26        CONCAT                                           ~20     ~19, '%0A'
         27        ECHO                                                     ~20
   24    28        PRE_INC                                                  !5
         29    >   COUNT                                            ~22     !1
         30        IS_SMALLER                                               !5, ~22
         31      > JMPNZ                                                    ~23, ->9
   22    32    >   PRE_INC                                                  !3
         33    >   COUNT                                            ~25     !0
         34        IS_SMALLER                                               !3, ~25
         35      > JMPNZ                                                    ~26, ->5
   29    36    > > RETURN                                                   null

End of function simplifyarrays

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.12 ms | 1407 KiB | 21 Q