Jump to content

Pascal's triangle: Difference between revisions

Add NetRexx implementation
m ('''See also:''' * Evaluate binomial coefficients)
(Add NetRexx implementation)
Line 1,442:
pascaltr(4);
end</lang>
 
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary
 
numeric digits 1000 -- allow very large numbers
printPascalTriangle(arg)
return
 
-- -----------------------------------------------------------------------------
method printPascalTriangle(arg) public static
parse arg rows .
if rows = '' then rows = 10 -- default to 10 rows
lines = ''
mx = (factorial(rows - 1) / factorial(rows % 2) / factorial(rows - 1 - rows % 2)).length() -- width of widest number
 
loop row = 1 to rows
n1 = 1.center(mx)
line = n1
loop col = 2 to row
n2 = col - 1
n1 = n1 * (row - n2) / n2
line = line n1.center(mx)
end col
lines[row] = line.strip()
end row
 
-- display triangle
ml = lines[rows].length() -- length of longest line
loop row = 1 to rows
say lines[row].centre(ml)
end row
 
return
 
-- -----------------------------------------------------------------------------
method factorial(n) public static
fac = 1
loop n_ = 2 to n
fac = fac * n_
end n_
return fac /*calc. factorial*/
</lang>
{{out}}
<pre>
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
</pre>
 
=={{header|Nial}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.