challenges

my solutions to various "programming challenge" problems
git clone https://wehaveforgeathome.hates.computer/challenges.git
Log | Files | Refs | LICENSE

004.php (639B)


      1 <?php
      2 
      3 /*
      4 Approach on 8/30/09: start with test if product is palindromic.
      5 */
      6 
      7 function isPalidromic ($n) {
      8   $string = strval($n);
      9   return $string == strrev($string);
     10 }
     11 
     12 function findPalidromic ($i, $j, $biggest) {
     13   $product = $i * $j;
     14   if (isPalidromic($product)) {
     15     if ($product > $biggest) {
     16       return findPalidromic($i - 1, $i - 1, $product);
     17     }
     18     return findPalidromic($i - 1, $i - 1, $biggest);
     19   }
     20   if ($i == 100) {
     21     return $biggest;
     22   }
     23   if ($product < $biggest) {
     24     return findPalidromic($i - 1, $i - 1, $biggest);
     25   }
     26   return findPalidromic($i, $j - 1, $biggest);
     27 }
     28 
     29 print findPalidromic(999,999,0) . "\n";