Commits

Shlomi Fish committed b64a43b

Changed the algorithm to eliminate dead-ends.

  • Participants
  • Parent commits ff291ad

Comments (0)

Files changed (1)

project-euler/88/euler-88.pl

         my $recurse;
 
         $recurse = sub {
-            my ($num_left, $min_i, $prod_so_far, $sum_left) = @_;
+            my ($num_left, $min_i, $product_left, $sum_left) = @_;
 
-            if ($prod_so_far > $sum)
+            if ($product_left < 1) 
             {
                 return;
             }
 
             if ($num_left == 1)
             {
-                return ($prod_so_far * $sum_left == $sum);
+                return ($product_left == $sum_left);
             }
             else
             {
                     {
                         last I_LOOP;
                     }
+                    if ($product_left % $i)
+                    {
+                        next I_LOOP;
+                    }
                     if ($recurse->(
-                        $num_left-1, $i, $prod_so_far*$i, $sum_left-$i
+                        $num_left-1, $i, $product_left / $i, $sum_left-$i
                     ))
                     {
                         return 1;
             }
         };
 
-        if ($recurse->($n, 1, 1, $sum))
+        if ($recurse->($n, 1, $sum, $sum))
         {
             return $sum;
         }