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(2, self::OneThousand, 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; } }
Output for git.master, git.master_jit, rfc.property-hooks
Sum of common factors: 3

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
47.94 ms | 401 KiB | 8 Q