Pascal's triangle: Difference between revisions

(Microsoft Small Basic)
Line 3,253:
sub pascal_line { $_[0] ? unpack "A(A6)*", 1000001**$_[0] : 1 }
sub pascal { print "@{[map -+-$_, pascal_line $_]}\n" for 0..$_[0]-1 }</lang>
 
This triangle is build using the 'sock' or 'hockey stick' pattern property. Here I use the word tartaglia and not pascal because in my country it's called after the Niccolò Fontana, known also as Tartaglia. A full graphical implementation of 16 properties that can be found in the triangle can be found at mine [https://github.com/LorenzoTa/Tartaglia-s-triangle Tartaglia's triangle]
 
<lang perl>
#!/usr/bin/perl
use strict;
use warnings;
{
my @tartaglia ;
sub tartaglia {
my ($x,$y) = @_;
if ($x == 0 or $y == 0) { $tartaglia[$x][$y]=1 ; return 1};
my $ret ;
foreach my $yps (0..$y){
$ret += ( $tartaglia[$x-1][$yps] || &tartaglia($x-1,$yps) );
}
$tartaglia[$x][$y] = $ret;
return $ret;
}
}
sub tartaglia_row {
my $y = shift;
my $x = 0;
my @row;
$row[0] = &tartaglia($x,$y+1);
foreach my $pos (0..$y-1) {push @row, &tartaglia(++$x,--$y)}
return @row;
}
 
 
for (0..5) {print join ' ', &tartaglia_row($_),"\n"}
print "\n\n";
 
 
print &tartaglia(3,3),"\n";
my @third = &tartaglia_row(5);
print "@third\n";
</lang>
 
which output
<pre>
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
 
 
20
1 5 10 10 5 1
</pre>
 
=={{header|Perl 6}}==
Anonymous user