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; 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); } 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); } 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; } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.40.0120.00919.10
8.3.30.0090.00619.17
8.3.20.0040.00420.46
8.3.10.0090.00923.48
8.3.00.0080.00019.23
8.2.170.0150.00722.96
8.2.160.0130.00720.55
8.2.150.0050.00324.18
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0160.00020.69
8.2.110.0030.00621.98
8.2.100.0030.00918.03
8.2.90.0040.00419.47
8.2.80.0080.00017.97
8.2.70.0000.01017.63
8.2.60.0040.00418.15
8.2.50.0090.00018.07
8.2.40.0060.00319.97
8.2.30.0030.00518.44
8.2.20.0000.00817.82
8.2.10.0050.00318.30
8.2.00.0030.00517.96
8.1.270.0040.00423.87
8.1.260.0060.00326.35
8.1.250.0040.00428.09
8.1.240.0030.00623.89
8.1.230.0090.00319.13
8.1.220.0030.00617.88
8.1.210.0000.00818.77
8.1.200.0090.00017.48
8.1.190.0000.00817.47
8.1.180.0000.00918.10
8.1.170.0170.00018.30
8.1.160.0040.00422.21
8.1.150.0070.00018.75
8.1.140.0000.00817.59
8.1.130.0030.00317.88
8.1.120.0040.00417.67
8.1.110.0040.00417.49
8.1.100.0000.00717.71
8.1.90.0040.00417.66
8.1.80.0060.00317.63
8.1.70.0000.00817.50
8.1.60.0030.00717.78
8.1.50.0060.00317.61
8.1.40.0040.00417.72
8.1.30.0080.00017.80
8.1.20.0040.00417.72
8.1.10.0040.00417.63
8.1.00.0050.00317.55
8.0.300.0000.00818.77
8.0.290.0050.00217.05
8.0.280.0000.00718.63
8.0.270.0050.00217.41
8.0.260.0070.00017.40
8.0.250.0050.00217.04
8.0.240.0000.00916.99
8.0.230.0080.00017.11
8.0.220.0040.00416.94
8.0.210.0000.00817.10
8.0.200.0000.00817.07
8.0.190.0040.00417.12
8.0.180.0040.00717.02
8.0.170.0040.00417.05
8.0.160.0040.00416.98
8.0.150.0070.00016.93
8.0.140.0000.00716.86
8.0.130.0060.00313.45
8.0.120.0060.00317.01
8.0.110.0080.00016.94
8.0.100.0050.00517.07
8.0.90.0000.00717.08
8.0.80.0120.00917.04
8.0.70.0080.00016.97
8.0.60.0030.00517.04
8.0.50.0030.00616.95
8.0.30.0140.00517.10
8.0.20.0160.00317.40
8.0.10.0040.00416.97
8.0.00.0090.01116.94
7.4.330.0000.00515.14
7.4.320.0030.00316.64
7.4.300.0000.00616.60
7.4.290.0000.00716.73
7.4.280.0000.00716.71
7.4.270.0040.00416.73
7.4.260.0000.01016.72
7.4.250.0000.00716.69
7.4.240.0010.00616.73
7.4.230.0040.00416.67
7.4.220.0170.01016.84
7.4.210.0100.00716.65
7.4.200.0040.00416.44
7.4.190.0040.00416.83
7.4.160.0160.00016.43
7.4.150.0170.00917.40
7.4.140.0100.00717.86
7.4.130.0140.00616.73
7.4.120.0070.01116.60
7.4.110.0130.01016.64
7.4.100.0130.00616.62
7.4.90.0090.00916.72
7.4.80.0150.01219.39
7.4.70.0120.00916.83
7.4.60.0070.01016.76
7.4.50.0000.00716.58
7.4.40.0070.01416.59
7.4.30.0070.01016.58
7.4.00.0030.01414.91
7.3.330.0000.00513.35
7.3.320.0030.00313.36
7.3.310.0030.00516.41
7.3.300.0000.00716.38
7.3.290.0140.00416.50
7.3.280.0100.01016.54
7.3.270.0100.00717.40
7.3.260.0120.00616.72
7.3.250.0030.01516.60
7.3.240.0110.00516.72
7.3.230.0130.00316.74
7.3.210.0120.00816.54
7.3.200.0060.01219.39
7.3.190.0070.01016.64
7.3.180.0070.01316.42
7.3.170.0150.00716.60
7.3.160.0100.00716.59
7.3.120.0040.01114.84
7.3.10.0100.00716.50
7.3.00.0040.01116.72
7.2.330.0120.00616.94
7.2.320.0160.01117.02
7.2.310.0170.00416.72
7.2.300.0070.01016.97
7.2.290.0060.01216.62
7.2.130.0080.00516.80
7.2.120.0070.01016.91
7.2.110.0060.01016.99
7.2.100.0000.01816.84
7.2.90.0060.00917.03
7.2.80.0090.00617.09
7.2.70.0060.00317.03
7.2.60.0070.01316.90
7.2.50.0070.00717.13
7.2.40.0030.01016.66
7.2.30.0060.00616.83
7.2.20.0040.00817.13
7.2.10.0030.00916.66
7.2.00.0020.00918.22
7.1.250.0060.00915.77
7.1.100.0040.00718.20
7.1.70.0060.00617.07
7.1.60.0110.01519.46
7.1.50.0040.01116.95
7.1.00.0000.08022.30
7.0.200.0100.00016.77
7.0.140.0070.07022.10
7.0.100.0300.08720.00
7.0.90.0030.08320.13
7.0.80.0400.07720.01
7.0.70.0130.07020.03
7.0.60.0070.07720.03
7.0.50.0100.07720.37
7.0.40.0030.08719.96
7.0.30.0070.07720.05
7.0.20.0100.08020.10
7.0.10.0170.06719.92
7.0.00.0070.07719.95
5.6.280.0030.07721.34
5.6.250.0030.07320.91
5.6.240.0130.04320.80
5.6.230.0100.05320.91
5.6.220.0100.07721.05
5.6.210.0030.08720.88
5.6.200.0100.08021.40
5.6.190.0070.08321.39
5.6.180.0070.08721.26
5.6.170.0030.06721.32
5.6.160.0130.07721.42
5.6.150.0100.07721.34
5.6.140.0030.07721.32
5.6.130.0130.07721.44
5.6.120.0070.08721.31
5.6.110.0130.07721.39
5.6.100.0130.04721.30
5.6.90.0100.08021.37
5.6.80.0070.08020.75
5.6.70.0030.08320.64
5.6.60.0070.08320.70
5.6.50.0130.05720.80
5.6.40.0200.05320.70
5.6.30.0030.08720.85
5.6.20.0100.06720.55
5.6.10.0130.07720.77
5.6.00.0100.07320.65
5.5.380.0030.05020.68
5.5.370.0070.07320.77
5.5.360.0100.08320.80
5.5.350.0030.09320.64
5.5.340.0070.09021.19
5.5.330.0030.09021.07
5.5.320.0130.07021.15
5.5.310.0130.08721.11
5.5.300.0130.07321.20
5.5.290.0070.08320.95
5.5.280.0130.04321.23
5.5.270.0030.09020.99
5.5.260.0100.09021.11
5.5.250.0070.08021.04
5.5.240.0030.08020.63
5.5.230.0130.07720.56
5.5.220.0130.08320.60
5.5.210.0100.08020.32
5.5.200.0030.08720.30
5.5.190.0100.07320.49
5.5.180.0070.08020.52
5.5.160.0130.07320.58
5.5.150.0070.08020.49
5.5.140.0070.07720.30
5.5.130.0170.06720.59
5.5.120.0000.08720.52
5.5.110.0230.04720.46
5.5.100.0200.07020.38
5.5.90.0030.09020.32
5.5.80.0100.07020.25
5.5.70.0070.08320.43
5.5.60.0070.07020.46
5.5.50.0070.04720.46
5.5.40.0070.08320.47
5.5.30.0030.04720.39
5.5.20.0000.04720.31
5.5.10.0130.03720.35
5.5.00.0070.03720.41
5.4.450.0100.08019.61
5.4.440.0100.06719.78
5.4.430.0070.06319.59
5.4.420.0100.07719.66
5.4.410.0100.07719.55
5.4.400.0070.08019.46
5.4.390.0130.07019.32
5.4.380.0070.08319.35
5.4.370.0030.08319.11
5.4.360.0100.07319.42
5.4.350.0030.07319.15
5.4.340.0200.06319.31
5.4.320.0070.06719.28
5.4.310.0070.07719.29
5.4.300.0030.08019.35
5.4.290.0200.06319.30
5.4.280.0100.07319.29
5.4.270.0130.07319.29
5.4.260.0000.07019.13
5.4.250.0070.06719.18
5.4.240.0130.06319.30
5.4.230.0070.07719.19
5.4.220.0130.06019.13
5.4.210.0030.04319.26
5.4.200.0070.06319.19
5.4.190.0070.06719.11
5.4.180.0030.07019.34
5.4.170.0070.06319.18
5.4.160.0070.07319.32
5.4.150.0130.07019.29
5.4.140.0030.03316.66
5.4.130.0070.07316.74
5.4.120.0170.03716.69
5.4.110.0030.06716.64
5.4.100.0100.07016.74
5.4.90.0070.07716.79
5.4.80.0100.06716.67
5.4.70.0170.06716.60
5.4.60.0130.07316.60
5.4.50.0100.07016.59
5.4.40.0130.06716.60
5.4.30.0070.07716.62
5.4.20.0070.07716.57
5.4.10.0030.08316.64
5.4.00.0070.08015.97
5.3.290.0100.07314.79
5.3.280.0070.07714.66
5.3.270.0070.07714.70
5.3.260.0070.03714.71
5.3.250.0130.03014.62
5.3.240.0100.07714.64
5.3.230.0070.08014.52
5.3.220.0030.08014.66
5.3.210.0070.08014.49
5.3.200.0030.07014.71
5.3.190.0130.07314.54
5.3.180.0130.07014.54
5.3.170.0130.06314.59
5.3.160.0070.07314.57
5.3.150.0070.07714.48
5.3.140.0070.08014.56
5.3.130.0100.07014.62
5.3.120.0030.08314.52
5.3.110.0100.08314.62
5.3.100.0070.07714.03
5.3.90.0070.07014.01
5.3.80.0000.08313.95
5.3.70.0100.07014.16
5.3.60.0170.07314.05
5.3.50.0030.04313.96
5.3.40.0030.04314.09
5.3.30.0100.07714.00
5.3.20.0170.06713.85
5.3.10.0070.06713.85
5.3.00.0100.06313.85

preferences:
43.97 ms | 400 KiB | 5 Q