Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

taxicabNumberTest

By: Wickwire on Nov 30th, 2012  |  syntax: PHP  |  size: 0.89 KB  |  views: 42  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. if ($argc != 2) {
  5.    echo "Usage cube.php <number>".PHP_EOL;
  6.    exit;
  7. }
  8. $num = $argv[1];
  9.  
  10. echo "Finding cubes less than ".$num.PHP_EOL;
  11.  
  12. $factor = 1;
  13. $arr = array();
  14. $bottom = array();
  15. $top = array();
  16. $half = floor($num/2);
  17. //Modified the below loop to parition the cubes while its generating them.
  18. //Thus preventing another loop to partition the cubes
  19. do {
  20.    $cube = pow($factor,3);
  21.    if ($cube <= $half) {
  22.       $bottom[$cube] = $factor;
  23.    } else {
  24.       $top[$factor] = $cube;
  25.    }
  26.    $factor++;
  27. } while ($cube < $num);
  28.  
  29. echo "Found ".count($top)." cubes > lower half of list".PHP_EOL;
  30. echo "Found ".count($bottom)." cubes <= lower half of list".PHP_EOL;
  31.  
  32. foreach ($top as $factor => $product) {
  33.    $remainder = $num-$product;
  34.    if (isset($bottom[$remainder])) {
  35.       echo "Found ".$factor."^3 + ".$bottom[$remainder]."^3 = ".$num.PHP_EOL;
  36.    }
  37. }
clone this paste RAW Paste Data