challenges

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

005.php (958B)


      1 <?php
      2 
      3 /* 8/31/09 */
      4 
      5 $n = 20;
      6 
      7 function listOfIntsToN ($n) {
      8   $ints = array();
      9   for ($i = 1; $i <= $n; $i++) {
     10     $ints[] = $i;
     11   }
     12   return $ints;
     13 }
     14 
     15 $ints = listofIntsToN($n);
     16 
     17 function isPrime($n) {
     18   $sqrt = sqrt($n);
     19   for ($i = 2; $i <= $sqrt; $i++) {
     20     if ($n % $i == 0) {
     21       return false;
     22     }
     23   }
     24   return true;
     25 }
     26 
     27 function findSolnRecursive ($product, $multiple, $ints) {
     28   if (count($ints) == 0) {
     29     return $product;
     30   }
     31 
     32   $filteredInts = array();
     33   foreach ($ints as $int) {
     34     if ($product % $int != 0) {
     35       $filteredInts[] = $int;
     36     }
     37   }
     38 
     39   if (count($filteredInts) < count($ints)) {
     40     return findSolnRecursive($product, $product, $filteredInts);
     41   }
     42 
     43   if (isPrime($ints[0])) {
     44     $product *= array_shift($ints);
     45     return findSolnRecursive($product, $product, $filteredInts);
     46   }
     47 
     48   $product += $multiple;
     49   return findSolnRecursive($product, $multiple, $filteredInts);
     50 }
     51 
     52 print findSolnRecursive(1, 1, $ints) . "\n";