Jump to content

Pascal's triangle: Difference between revisions

CoffeeScript
(Added DWScript)
(CoffeeScript)
Line 322:
[1]))
</lang>
 
=={{header|CoffeeScript}}==
This version assumes n is an integer and n >= 1. It efficiently computes binomial coefficients.
<lang coffeescript>
pascal = (n) ->
width = 6
for r in [1..n]
s = ws (width/2) * (n-r) # center row
output = (n) -> s += pad width, n
cell = 1
output cell
# Compute binomial coefficients as you go
# across the row.
for c in [1...r]
cell *= (r-c) / c
output cell
console.log s
 
ws = (n) ->
s = ''
s += ' ' for i in [0...n]
s
 
pad = (cnt, n) ->
s = n.toString()
# There is probably a better way to do this.
cnt -= s.length
right = Math.floor(cnt / 2)
left = cnt - right
ws(left) + s + ws(right)
 
pascal(7)
 
</lang>
 
output
<pre>
> coffee pascal.coffee
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
</pre>
 
=={{header|Common Lisp}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.