<?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);
//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 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);
}
}
Error : Call to undefined method IntelligentBee::splitNumbers()
Output for git.master_jit
Fatal error: Out of memory (allocated 2097152 bytes) (tried to allocate 16777224 bytes) in /in/iGEPe on line 57
mmap() failed: [12] Cannot allocate memory
mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.