#!/usr/bin/php
<?php
if ($argc != 2) {
echo "Usage cube.php <number>".PHP_EOL;
exit;
}
$num = $argv[1];
echo "Finding cubes less than ".$num.PHP_EOL;
$factor = 1;
$arr = array();
$bottom = array();
$top = array();
$half = floor($num/2);
//Modified the below loop to parition the cubes while its generating them.
//Thus preventing another loop to partition the cubes
do {
$cube = pow($factor,3);
if ($cube <= $half) {
$bottom[$cube] = $factor;
} else {
$top[$factor] = $cube;
}
$factor++;
} while ($cube < $num);
echo "Found ".count($top)." cubes > lower half of list".PHP_EOL;
echo "Found ".count($bottom)." cubes <= lower half of list".PHP_EOL;
foreach ($top as $factor => $product) {
$remainder = $num-$product;
if (isset($bottom[$remainder])) {
echo "Found ".$factor."^3 + ".$bottom[$remainder]."^3 = ".$num.PHP_EOL;
}
}