Commits

Shlomi Fish committed c1a7e44

Some samll optimisations.

It yielded the right solution but after a very long time. I found a better
way to do it in the forum.

Comments (0)

Files changed (1)

project-euler/88/euler-88.pl

 
     # print "(num_left=$num_left, min_i=$min_i, prod_so_far=$prod_so_far, sum_left=$sum_left) Sum=$sum\n";
 
-    if ($num_left == 1)
+    # 1*1*1*1*1*$product_left
+    if ($product_left+$num_left-1 == $sum_left)
     {
-        return ($product_left == $sum_left);
-    }
-    # Pad with 1's
-    elsif ($product_left == 1)
-    {
-        return ($sum_left == $num_left);
+        return 1;
     }
     else
     {
-        # 1*1*1*1*1*$product_left
-        if ($product_left+$num_left-1 == $sum_left)
-        {
-            return 1;
-        }
-
-        my $loop_max = min($max_i, ($product_left>>1));
-        my $loop_min = max(2, $sum_left / $num_left);
-
         I_LOOP:
-        for my $i (reverse($loop_min .. $loop_max))
+        for my $i (reverse(
+            max(2, $sum_left / $num_left)
+                ..
+            min($max_i, ($product_left>>1)))
+        )
         {
             if ($product_left % $i)
             {
 for my $n (2 .. 12_000)
 {
     my $val = smallest_product_n($n);
-    print "Reached $n = $val\n";
+    # print "Reached $n = $val\n";
     $numbers{$val}++;
+
+    if ($n % 100 == 0)
+    {
+        print "Reached $n = $val\n";
+    }
 }
 
 print "Sum = ", sum(keys(%numbers)), "\n";