@ 2014-07-02T06:27:55Z <?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, 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;
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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).
Version System time (s) User time (s) Memory (MiB) 8.3.6 0.020 0.024 16.50 8.3.5 0.009 0.018 21.96 8.3.4 0.011 0.011 18.96 8.3.3 0.017 0.017 19.34 8.3.2 0.006 0.016 20.51 8.3.1 0.007 0.007 23.64 8.3.0 0.017 0.003 20.64 8.2.18 0.007 0.013 18.25 8.2.17 0.007 0.014 22.96 8.2.16 0.010 0.021 20.68 8.2.15 0.006 0.012 24.18 8.2.14 0.000 0.018 24.66 8.2.13 0.003 0.009 26.16 8.2.12 0.003 0.006 20.92 8.2.11 0.003 0.010 21.85 8.2.10 0.009 0.009 17.78 8.2.9 0.007 0.010 19.47 8.2.8 0.003 0.007 17.97 8.2.7 0.007 0.003 17.75 8.2.6 0.003 0.010 18.05 8.2.5 0.003 0.008 18.07 8.2.4 0.003 0.006 20.07 8.2.3 0.006 0.003 18.58 8.2.2 0.003 0.010 18.08 8.2.1 0.006 0.003 18.44 8.2.0 0.007 0.003 17.89 8.1.28 0.016 0.010 25.92 8.1.27 0.004 0.008 23.92 8.1.26 0.007 0.004 26.35 8.1.25 0.012 0.000 28.09 8.1.24 0.007 0.007 23.70 8.1.23 0.010 0.014 19.30 8.1.22 0.000 0.009 18.02 8.1.21 0.006 0.006 18.77 8.1.20 0.007 0.011 17.60 8.1.19 0.004 0.008 17.61 8.1.18 0.008 0.003 18.10 8.1.17 0.000 0.010 18.70 8.1.16 0.003 0.010 22.18 8.1.15 0.006 0.012 18.81 8.1.14 0.000 0.017 17.68 8.1.13 0.009 0.000 18.03 8.1.12 0.003 0.008 17.75 8.1.11 0.003 0.013 17.76 8.1.10 0.000 0.010 17.80 8.1.9 0.000 0.018 17.74 8.1.8 0.003 0.006 17.71 8.1.7 0.004 0.015 17.64 8.1.6 0.006 0.006 17.88 8.1.5 0.006 0.011 17.74 8.1.4 0.003 0.010 17.81 8.1.3 0.004 0.014 17.86 8.1.2 0.003 0.017 17.86 8.1.1 0.003 0.006 17.83 8.1.0 0.005 0.005 17.82 8.0.30 0.003 0.009 18.77 8.0.29 0.000 0.011 17.30 8.0.28 0.003 0.013 18.60 8.0.27 0.006 0.003 17.34 8.0.26 0.000 0.011 17.33 8.0.25 0.004 0.008 17.12 8.0.24 0.004 0.011 17.12 8.0.23 0.000 0.016 17.02 8.0.22 0.007 0.010 17.17 8.0.21 0.003 0.010 16.98 8.0.20 0.003 0.009 17.11 8.0.19 0.000 0.010 17.05 8.0.18 0.000 0.020 17.12 8.0.17 0.006 0.006 17.00 8.0.16 0.007 0.010 17.10 8.0.15 0.010 0.003 17.04 8.0.14 0.004 0.014 17.12 8.0.13 0.004 0.004 13.54 8.0.12 0.004 0.014 17.00 8.0.11 0.000 0.009 17.05 8.0.10 0.004 0.014 16.98 8.0.9 0.004 0.008 16.84 8.0.8 0.006 0.016 17.04 8.0.7 0.003 0.007 16.84 8.0.6 0.003 0.006 16.91 8.0.5 0.004 0.011 16.93 8.0.3 0.012 0.031 17.37 8.0.2 0.019 0.024 17.46 8.0.1 0.003 0.006 17.08 8.0.0 0.011 0.039 17.09 7.4.33 0.004 0.004 15.00 7.4.32 0.003 0.012 16.61 7.4.30 0.004 0.004 16.68 7.4.29 0.006 0.012 16.80 7.4.28 0.003 0.006 16.68 7.4.27 0.004 0.004 16.61 7.4.26 0.006 0.021 16.72 7.4.25 0.007 0.007 16.66 7.4.24 0.006 0.005 16.67 7.4.23 0.000 0.011 16.58 7.4.22 0.007 0.040 16.68 7.4.21 0.007 0.040 16.78 7.4.20 0.006 0.012 16.91 7.4.19 0.000 0.018 16.70 7.4.16 0.007 0.033 16.80 7.4.15 0.018 0.026 17.40 7.4.14 0.010 0.049 17.86 7.4.13 0.011 0.029 16.72 7.4.12 0.011 0.028 16.75 7.4.11 0.008 0.035 16.70 7.4.10 0.006 0.040 16.88 7.4.9 0.009 0.039 16.70 7.4.8 0.010 0.040 19.39 7.4.7 0.007 0.034 16.68 7.4.6 0.007 0.035 16.94 7.4.5 0.007 0.013 16.47 7.4.4 0.013 0.032 16.49 7.4.3 0.006 0.035 16.69 7.4.0 0.007 0.013 15.31 7.3.33 0.003 0.014 13.46 7.3.32 0.004 0.004 13.61 7.3.31 0.007 0.013 16.41 7.3.30 0.007 0.007 16.55 7.3.29 0.010 0.048 16.54 7.3.28 0.010 0.036 16.58 7.3.27 0.014 0.031 17.40 7.3.26 0.010 0.040 16.56 7.3.25 0.007 0.039 16.79 7.3.24 0.012 0.018 16.63 7.3.23 0.004 0.025 16.54 7.3.21 0.014 0.034 16.57 7.3.20 0.013 0.045 19.39 7.3.19 0.007 0.036 16.75 7.3.18 0.013 0.043 16.64 7.3.17 0.014 0.011 16.43 7.3.16 0.006 0.029 16.77 7.3.12 0.009 0.015 14.86 7.3.1 0.010 0.027 16.17 7.3.0 0.015 0.006 16.11 7.2.33 0.006 0.047 17.00 7.2.32 0.018 0.046 16.57 7.2.31 0.010 0.020 16.81 7.2.30 0.007 0.035 16.82 7.2.29 0.013 0.035 16.59 7.2.13 0.018 0.012 16.62 7.2.12 0.016 0.016 16.63 7.2.11 0.012 0.009 16.48 7.2.10 0.031 0.017 16.48 7.2.9 0.013 0.027 16.46 7.2.8 0.014 0.006 16.52 7.2.7 0.007 0.017 16.57 7.2.6 0.024 0.003 16.87 7.2.5 0.020 0.016 16.39 7.2.4 0.029 0.017 16.90 7.2.3 0.016 0.008 16.65 7.2.2 0.019 0.022 16.75 7.2.1 0.018 0.004 16.46 7.2.0 0.008 0.024 18.10 7.1.25 0.013 0.026 15.51 7.1.10 0.003 0.009 18.05 7.1.7 0.000 0.010 17.10 7.1.6 0.013 0.017 19.46 7.1.5 0.003 0.014 17.25 7.0.20 0.007 0.004 16.86
preferences:dark mode live preview
43.35 ms | 400 KiB | 5 Q