3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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; protected static $dictionary = array( 0=>'zero', 1=>'one', 2=>'two', 3=>'three', 4=>'four', 5=>'five', 6=>'six', 7=>'seven', 8=>'eight', 9=>'nine', 10=>'ten', 11=>'eleven', 12=>'twelve', 13=>'thirteen', 14=>'fourteen', 15=>'fifthteen', 16=>'sixteen', 17=>'seventeen', 18=>'eighteen', 19=>'nineteen', 20=>'twenty', 30=>'thirty', 40=>'forty', 50=>'fifty', 60=>'sixty', 70=>'seventy', 80=>'eighty', 90=>'ninety', self::OneHundred=>'hundred', self::OneThousand=>'thousand', self::OneMillion=>'million', self::OneBillion=>'billion'//might get into memory issues ); 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::OneThousand); //split it into 2 arrays, one for odd numbers and one for even numbers to make it easier to perform each task $this->splitNumbers($arrayOfIntegers); } public function processOddNumbers() { foreach($this->oddNumbers as $oddNumber){ echo $this->convertIntToWord($oddNumber).PHP_EOL; } } 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 splitNumbers(array $arrayOfIntegers){ foreach($arrayOfIntegers as $int){ if($int & 1){ //odd number $this->oddNumbers[] = $int; }else{ //even number $this->evenNumbers[] = $int; } } sort($this->evenNumbers);//sort the even numbers as it helps calculate the closest number to 0 } 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; } protected function convertIntToWord($int) { $word = ''; if($int < 0){ $word .= 'minus ' . $this->convertIntToWord(abs($int)); } if(0 <= $int && $int < 20){ return self::$dictionary[$int]; } if(20 <= $int && $int < self::OneHundred){ return $this->processTens($int); } if (self::OneHundred <= $int && $int < self::OneThousand) { return $this->processHundreds($int); } if(self::OneThousand <= $int && $int < self::OneMillion){ return $this->processBigNumber($int, self::OneThousand); } if(self::OneMillion <= $int && $int < self::OneBillion){ return $this->processBigNumber($int, self::OneMillion); }else{ return $this->processBigNumber($int, self::OneBillion); } } protected function processTens($int) { $tens = intval($int/10)*10; $units = $int%10; $conv = self::$dictionary[$tens]; $conv .= $units > 0 ? '-'.self::$dictionary[$units] : ''; return $conv; } protected function processHundreds($int) { $hundreds = intval($int/100); $remainder = $int%100; $conv = self::$dictionary[$hundreds] . ' ' . self::$dictionary[self::OneHundred]; $conv .= $remainder > 0 ? " and " . $this->convertIntToWord($remainder) : ''; return $conv; } protected function processBigNumber($int, $baseUnit) { $nrBaseUnits = intval($int/$baseUnit); $remainder = $int%$baseUnit; $conv = $this->convertIntToWord($nrBaseUnits) . ' ' . self::$dictionary[$baseUnit]; $conv .= $remainder <= 0 ? "" : ($remainder < 100 ? " and " : ", ") . $this->convertIntToWord($remainder); return $conv; } }
Output for 5.4.0 - 7.1.0
three five seven nine eleven thirteen fifthteen seventeen nineteen twenty-one twenty-three twenty-five twenty-seven twenty-nine thirty-one thirty-three thirty-five thirty-seven thirty-nine forty-one forty-three forty-five forty-seven forty-nine fifty-one fifty-three fifty-five fifty-seven fifty-nine sixty-one sixty-three sixty-five sixty-seven sixty-nine seventy-one seventy-three seventy-five seventy-seven seventy-nine eighty-one eighty-three eighty-five eighty-seven eighty-nine ninety-one ninety-three ninety-five ninety-seven ninety-nine one hundred and one one hundred and three one hundred and five one hundred and seven one hundred and nine one hundred and eleven one hundred and thirteen one hundred and fifthteen one hundred and seventeen one hundred and nineteen one hundred and twenty-one one hundred and twenty-three one hundred and twenty-five one hundred and twenty-seven one hundred and twenty-nine one hundred and thirty-one one hundred and thirty-three one hundred and thirty-five one hundred and thirty-seven one hundred and thirty-nine one hundred and forty-one one hundred and forty-three one hundred and forty-five one hundred and forty-seven one hundred and forty-nine one hundred and fifty-one one hundred and fifty-three one hundred and fifty-five one hundred and fifty-seven one hundred and fifty-nine one hundred and sixty-one one hundred and sixty-three one hundred and sixty-five one hundred and sixty-seven one hundred and sixty-nine one hundred and seventy-one one hundred and seventy-three one hundred and seventy-five one hundred and seventy-seven one hundred and seventy-nine one hundred and eighty-one one hundred and eighty-three one hundred and eighty-five one hundred and eighty-seven one hundred and eighty-nine one hundred and ninety-one one hundred and ninety-three one hundred and ninety-five one hundred and ninety-seven one hundred and ninety-nine two hundred and one two hundred and three two hundred and five two hundred and seven two hundred and nine two hundred and eleven two hundred and thirteen two hundred and fifthteen two hundred and seventeen two hundred and nineteen two hundred and twenty-one two hundred and twenty-three two hundred and twenty-five two hundred and twenty-seven two hundred and twenty-nine two hundred and thirty-one two hundred and thirty-three two hundred and thirty-five two hundred and thirty-seven two hundred and thirty-nine two hundred and forty-one two hundred and forty-three two hundred and forty-five two hundred and forty-seven two hundred and forty-nine two hundred and fifty-one two hundred and fifty-three two hundred and fifty-five two hundred and fifty-seven two hundred and fifty-nine two hundred and sixty-one two hundred and sixty-three two hundred and sixty-five two hundred and sixty-seven two hundred and sixty-nine two hundred and seventy-one two hundred and seventy-three two hundred and seventy-five two hundred and seventy-seven two hundred and seventy-nine two hundred and eighty-one two hundred and eighty-three two hundred and eighty-five two hundred and eighty-seven two hundred and eighty-nine two hundred and ninety-one two hundred and ninety-three two hundred and ninety-five two hundred and ninety-seven two hundred and ninety-nine three hundred and one three hundred and three three hundred and five three hundred and seven three hundred and nine three hundred and eleven three hundred and thirteen three hundred and fifthteen three hundred and seventeen three hundred and nineteen three hundred and twenty-one three hundred and twenty-three three hundred and twenty-five three hundred and twenty-seven three hundred and twenty-nine three hundred and thirty-one three hundred and thirty-three three hundred and thirty-five three hundred and thirty-seven three hundred and thirty-nine three hundred and forty-one three hundred and forty-three three hundred and forty-five three hundred and forty-seven three hundred and forty-nine three hundred and fifty-one three hundred and fifty-three three hundred and fifty-five three hundred and fifty-seven three hundred and fifty-nine three hundred and sixty-one three hundred and sixty-three three hundred and sixty-five three hundred and sixty-seven three hundred and sixty-nine three hundred and seventy-one three hundred and seventy-three three hundred and seventy-five three hundred and seventy-seven three hundred and seventy-nine three hundred and eighty-one three hundred and eighty-three three hundred and eighty-five three hundred and eighty-seven three hundred and eighty-nine three hundred and ninety-one three hundred and ninety-three three hundred and ninety-five three hundred and ninety-seven three hundred and ninety-nine four hundred and one four hundred and three four hundred and five four hundred and seven four hundred and nine four hundred and eleven four hundred and thirteen four hundred and fifthteen four hundred and seventeen four hundred and nineteen four hundred and twenty-one four hundred and twenty-three four hundred and twenty-five four hundred and twenty-seven four hundred and twenty-nine four hundred and thirty-one four hundred and thirty-three four hundred and thirty-five four hundred and thirty-seven four hundred and thirty-nine four hundred and forty-one four hundred and forty-three four hundred and forty-five four hundred and forty-seven four hundred and forty-nine four hundred and fifty-one four hundred and fifty-three four hundred and fifty-five four hundred and fifty-seven four hundred and fifty-nine four hundred and sixty-one four hundred and sixty-three four hundred and sixty-five four hundred and sixty-seven four hundred and sixty-nine four hundred and seventy-one four hundred and seventy-three four hundred and seventy-five four hundred and seventy-seven four hundred and seventy-nine four hundred and eighty-one four hundred and eighty-three four hundred and eighty-five four hundred and eighty-seven four hundred and eighty-nine four hundred and ninety-one four hundred and ninety-three four hundred and ninety-five four hundred and ninety-seven four hundred and ninety-nine five hundred and one five hundred and three five hundred and five five hundred and seven five hundred and nine five hundred and eleven five hundred and thirteen five hundred and fifthteen five hundred and seventeen five hundred and nineteen five hundred and twenty-one five hundred and twenty-three five hundred and twenty-five five hundred and twenty-seven five hundred and twenty-nine five hundred and thirty-one five hundred and thirty-three five hundred and thirty-five five hundred and thirty-seven five hundred and thirty-nine five hundred and forty-one five hundred and forty-three five hundred and forty-five five hundred and forty-seven five hundred and forty-nine five hundred and fifty-one five hundred and fifty-three five hundred and fifty-five five hundred and fifty-seven five hundred and fifty-nine five hundred and sixty-one five hundred and sixty-three five hundred and sixty-five five hundred and sixty-seven five hundred and sixty-nine five hundred and seventy-one five hundred and seventy-three five hundred and seventy-five five hundred and seventy-seven five hundred and seventy-nine five hundred and eighty-one five hundred and eighty-three five hundred and eighty-five five hundred and eighty-seven five hundred and eighty-nine five hundred and ninety-one five hundred and ninety-three five hundred and ninety-five five hundred and ninety-seven five hundred and ninety-nine six hundred and one six hundred and three six hundred and five six hundred and seven six hundred and nine six hundred and eleven six hundred and thirteen six hundred and fifthteen six hundred and seventeen six hundred and nineteen six hundred and twenty-one six hundred and twenty-three six hundred and twenty-five six hundred and twenty-seven six hundred and twenty-nine six hundred and thirty-one six hundred and thirty-three six hundred and thirty-five six hundred and thirty-seven six hundred and thirty-nine six hundred and forty-one six hundred and forty-three six hundred and forty-five six hundred and forty-seven six hundred and forty-nine six hundred and fifty-one six hundred and fifty-three six hundred and fifty-five six hundred and fifty-seven six hundred and fifty-nine six hundred and sixty-one six hundred and sixty-three six hundred and sixty-five six hundred and sixty-seven six hundred and sixty-nine six hundred and seventy-one six hundred and seventy-three six hundred and seventy-five six hundred and seventy-seven six hundred and seventy-nine six hundred and eighty-one six hundred and eighty-three six hundred and eighty-five six hundred and eighty-seven six hundred and eighty-nine six hundred and ninety-one six hundred and ninety-three six hundred and ninety-five six hundred and ninety-seven six hundred and ninety-nine seven hundred and one seven hundred and three seven hundred and five seven hundred and seven seven hundred and nine seven hundred and eleven seven hundred and thirteen seven hundred and fifthteen seven hundred and seventeen seven hundred and nineteen seven hundred and twenty-one seven hundred and twenty-three seven hundred and twenty-five seven hundred and twenty-seven seven hundred and twenty-nine seven hundred and thirty-one seven hundred and thirty-three seven hundred and thirty-five seven hundred and thirty-seven seven hundred and thirty-nine seven hundred and forty-one seven hundred and forty-three seven hundred and forty-five seven hundred and forty-seven seven hundred and forty-nine seven hundred and fifty-one seven hundred and fifty-three seven hundred and fifty-five seven hundred and fifty-seven seven hundred and fifty-nine seven hundred and sixty-one seven hundred and sixty-three seven hundred and sixty-five seven hundred and sixty-seven seven hundred and sixty-nine seven hundred and seventy-one seven hundred and seventy-three seven hundred and seventy-five seven hundred and seventy-seven seven hundred and seventy-nine seven hundred and eighty-one seven hundred and eighty-three seven hundred and eighty-five seven hundred and eighty-seven seven hundred and eighty-nine seven hundred and ninety-one seven hundred and ninety-three seven hundred and ninety-five seven hundred and ninety-seven seven hundred and ninety-nine eight hundred and one eight hundred and three eight hundred and five eight hundred and seven eight hundred and nine eight hundred and eleven eight hundred and thirteen eight hundred and fifthteen eight hundred and seventeen eight hundred and nineteen eight hundred and twenty-one eight hundred and twenty-three eight hundred and twenty-five eight hundred and twenty-seven eight hundred and twenty-nine eight hundred and thirty-one eight hundred and thirty-three eight hundred and thirty-five eight hundred and thirty-seven eight hundred and thirty-nine eight hundred and forty-one eight hundred and forty-three eight hundred and forty-five eight hundred and forty-seven eight hundred and forty-nine eight hundred and fifty-one eight hundred and fifty-three eight hundred and fifty-five eight hundred and fifty-seven eight hundred and fifty-nine eight hundred and sixty-one eight hundred and sixty-three eight hundred and sixty-five eight hundred and sixty-seven eight hundred and sixty-nine eight hundred and seventy-one eight hundred and seventy-three eight hundred and seventy-five eight hundred and seventy-seven eight hundred and seventy-nine eight hundred and eighty-one eight hundred and eighty-three eight hundred and eighty-five eight hundred and eighty-seven eight hundred and eighty-nine eight hundred and ninety-one eight hundred and ninety-three eight hundred and ninety-five eight hundred and ninety-seven eight hundred and ninety-nine nine hundred and one nine hundred and three nine hundred and five nine hundred and seven nine hundred and nine nine hundred and eleven nine hundred and thirteen nine hundred and fifthteen nine hundred and seventeen nine hundred and nineteen nine hundred and twenty-one nine hundred and twenty-three nine hundred and twenty-five nine hundred and twenty-seven nine hundred and twenty-nine nine hundred and thirty-one nine hundred and thirty-three nine hundred and thirty-five nine hundred and thirty-seven nine hundred and thirty-nine nine hundred and forty-one nine hundred and forty-three nine hundred and forty-five nine hundred and forty-seven nine hundred and forty-nine nine hundred and fifty-one nine hundred and fifty-three nine hundred and fifty-five nine hundred and fifty-seven nine hundred and fifty-nine nine hundred and sixty-one nine hundred and sixty-three nine hundred and sixty-five nine hundred and sixty-seven nine hundred and sixty-nine nine hundred and seventy-one nine hundred and seventy-three nine hundred and seventy-five nine hundred and seventy-seven nine hundred and seventy-nine nine hundred and eighty-one nine hundred and eighty-three nine hundred and eighty-five nine hundred and eighty-seven nine hundred and eighty-nine nine hundred and ninety-one nine hundred and ninety-three nine hundred and ninety-five nine hundred and ninety-seven nine hundred and ninety-nine Sum of common factors: 3
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/Thm8V on line 33
Process exited with code 255.