3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * A code sample which converts odd integers to word strings and returns the sum of the common factors of even numbers. * * The input is assumed to be a random list of integers. For the sum of the common factors of even numbers * only positive values are taken into consideration */ error_reporting(E_ALL); function exception_handler($exception) { echo get_class($exception). ' : ' . $exception->getMessage(), "\n"; } set_exception_handler('exception_handler'); //start $iB = new IntelligentBee(array(6,12,48, 7, 101, 67)); //$iB->processOddNumbers(); $sum = $iB->processEvenNumbers(); echo PHP_EOL.'Sum of common factors: ' . $sum . PHP_EOL; //end class IntelligentBee{ const OneHundred = 100; const OneThousand = 1000; const OneMillion = 1000000; const OneBillion = 1000000000; protected $minCommonFactors = []; protected $oddNumbers = []; protected $evenNumbers = []; protected $startInt; protected $endInt; public function __construct(array $arrayOfIntegers){ $this->evenNumbers = range(16, self::OneMillion, 2); } public function processEvenNumbers() { $commonFactors = $this->calcFactors($this->getClosestEvenToZero($this->startInt, $this->endInt, $this->evenNumbers)); foreach($this->evenNumbers as $evenNumber){ $commonFactors = $this->checkIfHasCommonFactors($evenNumber, $commonFactors); } return array_sum($commonFactors); } protected function getClosestEvenToZero($startInt, $endInt, $evenNumbers){ $dist = abs($evenNumbers[0]); $idx = 0; $totalNo = count($evenNumbers); for($i=0;$i<$totalNo;$i++){ $newDist = abs($evenNumbers[$i]); if($newDist < $dist){ $idx = $i; $dist = $newDist; } if($dist == 0){//only if evenNumbers is sorted return $evenNumbers[$idx]; } } return $evenNumbers[$idx]; } protected function checkIfHasCommonFactors($int, array $minCommonFactors){ $int = abs($int); $cF = $this->calcFactors($int); return array_intersect($cF, $minCommonFactors); } private function calcFactors($int) { $int = abs($int); $cF = []; $max = $int/2; for($i=1;$i<=$max;$i++){ if(0 === $int%$i){ $cF[] = $i; } } $cF[] = $int; return $cF; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TQQQS
function name:  (null)
number of ops:  17
compiled vars:  !0 = $iB, !1 = $sum
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   INIT_FCALL                                               'error_reporting'
          1        SEND_VAL                                                 32767
          2        DO_ICALL                                                 
   16     3        INIT_FCALL                                               'set_exception_handler'
          4        SEND_VAL                                                 'exception_handler'
          5        DO_ICALL                                                 
   20     6        NEW                                              $4      'IntelligentBee'
          7        SEND_VAL_EX                                              <array>
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !0, $4
   22    10        INIT_METHOD_CALL                                         !0, 'processEvenNumbers'
         11        DO_FCALL                                      0  $7      
         12        ASSIGN                                                   !1, $7
   23    13        CONCAT                                           ~9      '%0ASum+of+common+factors%3A+', !1
         14        CONCAT                                           ~10     ~9, '%0A'
         15        ECHO                                                     ~10
  101    16      > RETURN                                                   1

Function exception_handler:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TQQQS
function name:  exception_handler
number of ops:  9
compiled vars:  !0 = $exception
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        GET_CLASS                                        ~1      !0
          2        CONCAT                                           ~2      ~1, '+%3A+'
          3        INIT_METHOD_CALL                                         !0, 'getMessage'
          4        DO_FCALL                                      0  $3      
          5        CONCAT                                           ~4      ~2, $3
          6        ECHO                                                     ~4
          7        ECHO                                                     '%0A'
   15     8      > RETURN                                                   null

End of function exception_handler

Class IntelligentBee:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TQQQS
function name:  __construct
number of ops:  9
compiled vars:  !0 = $arrayOfIntegers
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   43     1        INIT_FCALL                                               'range'
          2        SEND_VAL                                                 16
          3        SEND_VAL                                                 1000000
          4        SEND_VAL                                                 2
          5        DO_ICALL                                         $2      
          6        ASSIGN_OBJ                                               'evenNumbers'
          7        OP_DATA                                                  $2
   45     8      > RETURN                                                   null

End of function __construct

Function processevennumbers:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 24
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 24
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/TQQQS
function name:  processEvenNumbers
number of ops:  30
compiled vars:  !0 = $commonFactors, !1 = $evenNumber
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   INIT_METHOD_CALL                                         'calcFactors'
          1        INIT_METHOD_CALL                                         'getClosestEvenToZero'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $2      'startInt'
          4        SEND_FUNC_ARG                                            $2
          5        CHECK_FUNC_ARG                                           
          6        FETCH_OBJ_FUNC_ARG                               $3      'endInt'
          7        SEND_FUNC_ARG                                            $3
          8        CHECK_FUNC_ARG                                           
          9        FETCH_OBJ_FUNC_ARG                               $4      'evenNumbers'
         10        SEND_FUNC_ARG                                            $4
         11        DO_FCALL                                      0  $5      
         12        SEND_VAR_NO_REF_EX                                       $5
         13        DO_FCALL                                      0  $6      
         14        ASSIGN                                                   !0, $6
   52    15        FETCH_OBJ_R                                      ~8      'evenNumbers'
         16      > FE_RESET_R                                       $9      ~8, ->24
         17    > > FE_FETCH_R                                               $9, !1, ->24
   53    18    >   INIT_METHOD_CALL                                         'checkIfHasCommonFactors'
         19        SEND_VAR_EX                                              !1
         20        SEND_VAR_EX                                              !0
         21        DO_FCALL                                      0  $10     
         22        ASSIGN                                                   !0, $10
   52    23      > JMP                                                      ->17
         24    >   FE_FREE                                                  $9
   56    25        INIT_FCALL                                               'array_sum'
         26        SEND_VAR                                                 !0
         27        DO_ICALL                                         $12     
         28      > RETURN                                                   $12
   57    29*     > RETURN                                                   null

End of function processevennumbers

Function getclosesteventozero:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 13
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 13
Branch analysis from position: 29
Branch analysis from position: 13
Branch analysis from position: 22
filename:       /in/TQQQS
function name:  getClosestEvenToZero
number of ops:  32
compiled vars:  !0 = $startInt, !1 = $endInt, !2 = $evenNumbers, !3 = $dist, !4 = $idx, !5 = $totalNo, !6 = $i, !7 = $newDist
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   61     3        INIT_FCALL                                               'abs'
          4        FETCH_DIM_R                                      ~8      !2, 0
          5        SEND_VAL                                                 ~8
          6        DO_ICALL                                         $9      
          7        ASSIGN                                                   !3, $9
   62     8        ASSIGN                                                   !4, 0
   64     9        COUNT                                            ~12     !2
         10        ASSIGN                                                   !5, ~12
   65    11        ASSIGN                                                   !6, 0
         12      > JMP                                                      ->27
   66    13    >   INIT_FCALL                                               'abs'
         14        FETCH_DIM_R                                      ~15     !2, !6
         15        SEND_VAL                                                 ~15
         16        DO_ICALL                                         $16     
         17        ASSIGN                                                   !7, $16
   67    18        IS_SMALLER                                               !7, !3
         19      > JMPZ                                                     ~18, ->22
   68    20    >   ASSIGN                                                   !4, !6
   69    21        ASSIGN                                                   !3, !7
   71    22    >   IS_EQUAL                                                 !3, 0
         23      > JMPZ                                                     ~21, ->26
   72    24    >   FETCH_DIM_R                                      ~22     !2, !4
         25      > RETURN                                                   ~22
   65    26    >   PRE_INC                                                  !6
         27    >   IS_SMALLER                                               !6, !5
         28      > JMPNZ                                                    ~24, ->13
   76    29    >   FETCH_DIM_R                                      ~25     !2, !4
         30      > RETURN                                                   ~25
   77    31*     > RETURN                                                   null

End of function getclosesteventozero

Function checkifhascommonfactors:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TQQQS
function name:  checkIfHasCommonFactors
number of ops:  16
compiled vars:  !0 = $int, !1 = $minCommonFactors, !2 = $cF
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   80     2        INIT_FCALL                                               'abs'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !0, $3
   81     6        INIT_METHOD_CALL                                         'calcFactors'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $5      
          9        ASSIGN                                                   !2, $5
   83    10        INIT_FCALL                                               'array_intersect'
         11        SEND_VAR                                                 !2
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $7      
         14      > RETURN                                                   $7
   84    15*     > RETURN                                                   null

End of function checkifhascommonfactors

Function calcfactors:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 10
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 10
Branch analysis from position: 18
Branch analysis from position: 10
Branch analysis from position: 15
filename:       /in/TQQQS
function name:  calcFactors
number of ops:  22
compiled vars:  !0 = $int, !1 = $cF, !2 = $max, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
   88     1        INIT_FCALL                                               'abs'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $4      
          4        ASSIGN                                                   !0, $4
   89     5        ASSIGN                                                   !1, <array>
   90     6        DIV                                              ~7      !0, 2
          7        ASSIGN                                                   !2, ~7
   91     8        ASSIGN                                                   !3, 1
          9      > JMP                                                      ->16
   92    10    >   MOD                                              ~10     !0, !3
         11        IS_IDENTICAL                                             ~10, 0
         12      > JMPZ                                                     ~11, ->15
   93    13    >   ASSIGN_DIM                                               !1
         14        OP_DATA                                                  !3
   91    15    >   PRE_INC                                                  !3
         16    >   IS_SMALLER_OR_EQUAL                                      !3, !2
         17      > JMPNZ                                                    ~14, ->10
   96    18    >   ASSIGN_DIM                                               !1
         19        OP_DATA                                                  !0
   98    20      > RETURN                                                   !1
   99    21*     > RETURN                                                   null

End of function calcfactors

End of class IntelligentBee.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.48 ms | 1408 KiB | 25 Q