Matt Owen

source code for "files/pe/pe_053.pl"

return to portfolio
  1.  #!/usr/bin/perl
  2.  
  3.  use strict;
  4.  use warnings;
  5.  
  6.  
  7.  # get tri pos
  8.  sub tri_pos {
  9.   my ($n, $r) = @_;
  10.   return $n*($n+1)/2+$r;
  11.  }
  12.  
  13.  # get C(n, r)
  14.  sub get_tri {
  15.   my ($tri, $n, $r) = @_;
  16.   return $tri->[tri_pos $n, $r];
  17.  }
  18.  
  19.  # init
  20.  my ($pascals) = [1, 1, 1];
  21.  
  22.  
  23.  # fill pascals...
  24.  # this function may come in useful again someday...
  25.  # since it avoid having to divide large numbers to produce C(N,R)
  26.  for my $row (2..100) {
  27.   # place a 1 on the way left
  28.   push @$pascals, 1;
  29.  
  30.   # fill in the gap
  31.   push @$pascals, get_tri($pascals, $row-1, $_)+get_tri($pascals, $row-1, $_-1) for 1..$row-1;
  32.  
  33.   # place a 1 on the right
  34.   push @$pascals, 1;
  35.  }
  36.  
  37.  
  38.  print int grep { $_ >= 10**6 } @$pascals;
  39.  print "\n";