Pascal's triangle: Difference between revisions

Added Perl and Python.
m (mwn3d corrected me.)
(Added Perl and Python.)
Line 213:
}
}</java>
 
=={{header|Perl}}==
<perl>sub pascal
# Prints out $n rows of Pascal's triangle. It returns undef for
# failure and 1 for success.
{my $n = shift;
$n < 1 and return undef;
print "1\n";
$n == 1 and return 1;
my @last = (1);
foreach my $row (1 .. $n - 1)
{my @this = map {$last[$_] + $last[$_ + 1]} 0 .. $row - 2;
@last = (1, @this, 1);
print join(' ', @last), "\n";}
return 1;}</perl>
 
=={{header|Python}}==
<python>def pascal(n):
"""Prints out n rows of Pascal's triangle.
 
It returns False for failure and True for success."""
if n < 1: return False
print 1
if n == 1: return True
last = [1]
for row in range(1, n):
this = [last[i] + last[i + 1] for i in range(row - 1)]
last = [1] + this + [1]
print " ".join(map(str, last))
return True</python>
 
=={{header|Ruby}}==
845

edits