Commits

Shlomi Fish committed a6cd840

Add the solution for Euler #94.

Comments (0)

Files changed (1)

project-euler/94/euler-94-almost-equilateral-triangles.pl

 use strict;
 use warnings;
 
-use Math::BigFloat lib => "GMP", ":constant";
+use Math::BigInt lib => "GMP", ":constant";
 use IO::Handle;
 
 STDOUT->autoflush(1);
 # integral.
 # 
 
-my $sum = 0;
+# For No. 1, for "n" we need s = (3n-1)/2 
+# area = sqrt{ (3n-1) / 2 * (n-1) / 2 * (n-1) / 2 * (n+1) / 2 }
+#      = 1/4 * (n-1) * sqrt { (3n-1)(n+1) }
+
+# Let's do m = n+1, then we get 1/4 * (m-2) * sqrt { (3m-4)m } 
+
+# For No. 2, for "n" we need s = (3n+1)/2
+# area = sqrt{ (3n+1) /2 * (n+1) / 2 * (n+1) / 2 * (n-1) / 2 }
+#      = 1/4 * (n+1) * sqrt { (3n+1)(n-1) }
+
+=begin Hello
 
 foreach my $n (1 .. 1_000_000_000)
 {
-    my $v = (($n-1) * sqrt($n**2 - (($n-1)/2)**2));
+    my $v = sqrt((3*$n+1)*($n-1));
+
     if (int($v) == $v)
     {
-        print $n, "\n";
+        print "$n\n";
     }
 }
+
+See:
+
+http://www.research.att.com/~njas/sequences/?q=1%2C5%2C65%2C901%2C12545&sort=0&fmt=0&language=english&go=Search
+
+http://www.research.att.com/~njas/sequences/?q=1%2C17%2C241%2C3361%2C46817%2C652081&sort=0&fmt=0&language=english&go=Search
+
+=end Hello
+
+=cut
+
+my $sum = 0;
+for my $n1 (
+split(/,\s*/, q{17, 241, 3361, 46817, 652081, 9082321, 126500417})
+)
+{
+    print "$sum\n";
+    $sum += Math::BigInt->new($n1) * 3 -1;
+}
+
+for my $n2 (
+    split(/,\s*/, q{5, 65, 901, 12545, 174725, 2433601, 33895685})
+)
+{
+    print "$sum\n";
+    $sum += Math::BigInt->new($n2) * 3 + 1;
+}
+
+print "$sum\n";
+
+#    my $v = (($n-1) * sqrt($n**2 - (($n-1)/2)**2));
+#    if (int($v) == $v)
+#    {
+#        print $n, "\n";
+#    }