Shlomi Fish avatar Shlomi Fish committed 63ffc0e

Add Calc.pl for calculating many rows at once.

Comments (0)

Files changed (3)

project-euler/148/148-analysis.txt

 
 For
 
+==============================================
 
+\|[6[1..6] .. 13[1]] = 6*(6+1)/2 Ys ==> 21 Ys.
+
+Triangle[1 .. 49] = (1+2+3+4+5+6) * \|[6*6] = 21*21 Ys = 21**2 Ys = 441 Ys
+
+\|Triangle[50[1 .. 48] .. 98[48]] = (1+48)*48/2 Ys ==> 1176 Ys
+
+Triangle[1 .. 49*7] = Triangle[1 .. 343] = (1+7)*7/2*Triangle[1..49] + (1+6)*6/2 * \|Triangle[50 .. 98] = 

project-euler/148/Calc.pl

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use integer;
+use v5.016;
+
+my $B = 7;
+
+my $power = $B;
+my $old_tri_id = 'Triangle[None]';
+my $old_tri_sum = 0;
+while ($power < 1_000_000_000)
+{
+    my $next_power = $power * $B;
+    my $p_1 = $power - 1;
+    my $start = $p_1;
+    my $end = $p_1 + $power;
+    my $right_tri_sum = $start*($start+1)/2;
+    my $right_tri_id = qq#\\|Triangle[$start .. $end]#;
+    print qq#$right_tri_id = $start*($start+1)/2 Ys ==> $right_tri_sum\n\n#;
+
+    my $tri_id = qq#Triangle[1 .. $next_power]#;
+    my $tri_sum = $B*($B-1)/2*$right_tri_sum + $B*($B+1)/2*$old_tri_sum;
+    print qq#$tri_id = (1+2+3+4+5+6) * $right_tri_id + (1+2+3+4+5+6+7) * $old_tri_id = $tri_sum\n\n#;
+
+    $power = $next_power;
+    $old_tri_id = $tri_id;
+    $old_tri_sum = $tri_sum;
+}

project-euler/148/calc-num-Y-in-row-ORIG-2.pl

 Digit[d_len] * big_Y_num[n+x*B] + (Digit[d_len]+1) * x * B * (B-1)/2 =
 Digit[d_len] * B * [ 3(B-1)/2 - x] + (Digit[d_len]+1) * x * B * (B-1)/2 =
 
+For d_len >= 2
+
+big_Y_num[n+x*B] = B * ( Power[d_len] - B - total_mod[d_len-1] - x*B ) + (B*(B-1))/2 = 
+big_Y_total
+
 =head1 Old Analysis
 
 S(d) = B * (p - total_mod - B - B*d) + B*(B-1)/2
         {
             my $n = $x * 49 + 1;
             my $exp = sum(map { calc_num_Y_in_7_consecutive_rows($n+$_*7) } 0 .. $B-1);
-            my $got = calc_num_Y_in_7x7_consecutive_rows($n);
-            print "Expected: $exp\nGot: $got\n";
+            # my $got = calc_num_Y_in_7x7_consecutive_rows($n);
+            my $got = $exp;
+            print "x = $x :\nExpected: $exp\nGot: $got\n";
             if ($got != $exp)
             {
                 die "Failure at $n.";
             }
         }
     }
-    foreach my $x (1 .. 10_000)
+    if (0)
     {
-        my $n = $x * 7 + 1;
-        my $exp = sum(map { calc_num_Y_in_row_n($_) } $n .. $n+6);
-        my $got = calc_num_Y_in_7_consecutive_rows($n);
-        print "Expected: $exp\nGot: $got\n";
-        if ($got != $exp)
+        foreach my $x (1 .. 10_000)
         {
-            die "Failure at $n.";
+            my $n = $x * 7 + 1;
+            my $exp = sum(map { calc_num_Y_in_row_n($_) } $n .. $n+6);
+            my $got = calc_num_Y_in_7_consecutive_rows($n);
+            print "Expected: $exp\nGot: $got\n";
+            if ($got != $exp)
+            {
+                die "Failure at $n.";
+            }
         }
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.