Commits

Shlomi Fish committed 39e1494

Solved Euler #126 (finally).

The final, better, approach is in take 4.

Comments (0)

Files changed (1)

project-euler/126/euler-126-take4.pl

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use IO::Handle;
+
+use integer;
+
+STDOUT->autoflush(1);
+
+my @C;
+
+# Used to be:
+# <<<
+# Matches $X,$Y,$Z (where $X >= $Y >= $Z) to the cuboid array and maximal 
+# reached layer.
+# >>>
+# Now we no longer need the $X,$Y,$Z.
+#
+# Since the counts are divisible by 2 and are kept multiplied by 2, we
+# keep their halfs.
+
+my $max_C_n = 0;
+
+my $LIMIT = 50_000;
+
+my $z = 1;
+my $z_count = 1;
+
+while ($z_count)
+{
+    $z_count = 0;
+    print "Checking z=$z\n";
+    Y_LOOP:
+    for my $y (1 .. $z)
+    {
+        X_LOOP:
+        for my $x (1 .. $y)
+        {
+            my $new_layer_count = 
+                ($x*($y+$z)+$z*$y);
+
+            my $delta = ((($x+$y+$z)<<1)-4);
+
+            if ($new_layer_count >= $LIMIT)
+            {
+                if ($x == 1)
+                {
+                    last Y_LOOP;
+                }
+                else
+                {
+                    last X_LOOP;
+                }
+            }
+            while ($new_layer_count < $LIMIT)
+            {
+                $z_count++;
+                $C[$new_layer_count]++;
+                $new_layer_count += ($delta += 4);
+            }
+        }
+    }
+}
+continue
+{
+    $z++;
+}
+
+foreach my $count (1 .. $#C)
+{
+    if (defined($C[$count]))
+    {
+        print "C[" , ($count*2), "] = $C[$count]\n";
+    }
+}