Commits

Shlomi Fish committed 7b971e9

Add more.

Comments (0)

Files changed (2)

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

     return fact($n) / (fact($n-$k) * fact($k));
 }
 
+my $BASE = 7;
+
+sub calc_num_Y_in_row_n
+{
+    my $n_proto = shift;
+    my $n = $n_proto - 1;
+
+    my @digits;
+
+    my $digit_n = $n->copy();
+    while ($digit_n)
+    {
+        push @digits, $digit_n % $BASE;
+        $digit_n /= $BASE;
+    }
+
+    if (@digits <= 1)
+    {
+        return 0;
+    }
+    elsif (@digits == 2)
+    {
+        return ($BASE-1-$digits[0]) * $digits[1];
+    }
+    else
+    {
+        die "Cannot handle.";
+    }
+}
+
 my $n = shift(@ARGV);
 
-print join(' ', map { +(nCr($n+1, $_) % 7 == 0 ) ? 'Y' : 'N' } (1 .. $n)), "\n";
+print calc_num_Y_in_row_n($n), "\n";

project-euler/148/print-row.pl

 
 my $n = shift(@ARGV);
 
-print join(' ', map { +(nCr($n, $_) % 7 == 0 ) ? 'Y' : 'N' } (1 .. $n-1)), "\n";
+print join(' ', map { +(nCr($n-1, $_) % 7 == 0 ) ? 'Y' : 'N' } (1 .. $n-2)), "\n";