Advertisement
Wickwire

taxicabNumberTest

Nov 30th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.89 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement