Pascal's triangle: Difference between revisions

No edit summary
Line 11:
'''See also:'''
* [[Evaluate binomial coefficients]]
=={{header|8th}}==
One way, using array operations:
<lang forth>
\ print the array
: .arr \ a -- a
( . space ) a:each ;
 
: pasc \ a --
\ print the row
.arr cr
dup
\ create two rows from the first, one with a leading the other with a trailing 0
[0] 0 a:insert swap 0 a:push
\ add the arrays together to make the new one
' n:+ a:op ;
 
\ print the first 16 rows:
[1] ' pasc 16 times
</lang>
 
Another way, using the relation between element 'n' and element 'n-1' in a row:
<lang forth>
: ratio \ m n -- num denom
tuck n:- n:1+ swap ;
 
\ one item in the row: n m
: pascitem \ n m -- n
r@ swap
ratio
n:*/ n:round int
dup . space ;
 
\ One row of Pascal's triangle
: pascline \ n --
>r 1 int dup . space
' pascitem
1 r@ loop rdrop drop cr ;
 
\ Calculate the first 'n' rows of Pascal's triangle:
: pasc \ n
' pascline 0 rot loop cr ;
 
15 pasc
</lang>
=={{header|Ada}}==
 
Anonymous user