source code for "files/pe/pe_053.pl"
return to portfolio
- #!/usr/bin/perl
- use strict;
- use warnings;
- # get tri pos
- sub tri_pos {
- my ($n, $r) = @_;
- return $n*($n+1)/2+$r;
- }
- # get C(n, r)
- sub get_tri {
- my ($tri, $n, $r) = @_;
- return $tri->[tri_pos $n, $r];
- }
- # init
- my ($pascals) = [1, 1, 1];
- # fill pascals...
- # this function may come in useful again someday...
- # since it avoid having to divide large numbers to produce C(N,R)
- for my $row (2..100) {
- # place a 1 on the way left
- push @$pascals, 1;
- # fill in the gap
- push @$pascals, get_tri($pascals, $row-1, $_)+get_tri($pascals, $row-1, $_-1) for 1..$row-1;
- # place a 1 on the right
- push @$pascals, 1;
- }
- print int grep { $_ >= 10**6 } @$pascals;
- print "\n";