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; $a = $iB->getCmmdcSum(); echo PHP_EOL.' Sum is ' . $a[1]. 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){ $startInt = min($arrayOfIntegers); $endInt = max($arrayOfIntegers); if(abs($startInt) > self::OneBillion || abs($startInt) > self::OneBillion){ throw new OutOfRangeException(sprintf("We can only process numbers between %s and %s! %s provided!", self::OneBillion, self::OneBillion, $startInt)); } if(abs($endInt) > self::OneBillion || abs($endInt) > self::OneBillion){ throw new OutOfRangeException(sprintf("We can only process numbers between %s and %s! %s provided!", self::OneBillion, self::OneBillion, $startInt)); } $this->startInt = $startInt; $this->endInt = $endInt; $arrayOfIntegers = range(2, self::OneMillion,2); } public function getCmmdcSum() { $values = $this->evenNumbers; $num_values = count($values); $x = current($values); $y = next($values); for ($i = 1; $i < $num_values; $i ++) { $a = max($x, $y); $b = min($x, $y); $c= 1; do { $c = $a % $b; $gcf = $b; $a = $b; $b = $c; } while ($c != 0); $x = $gcf; $y = next($values); } $sum = 0; for($i = 1; $i <= $gcf; $i ++){ if(0 === $gcf % $i){ $sum += $i; } } return array($gcf, $sum); } }

preferences:
31.7 ms | 402 KiB | 5 Q