Pascal's triangle: Difference between revisions

jq
m (→‎{{header|REXX}}: removed OVERFLOW from PRE html tags.)
(jq)
Line 1,575:
1 7 15 23 23 15 7 1
</pre>
 
=={{header|jq}}==
{{works with|jq|1.4}}
pascal(n) as defined here produces a stream of n arrays,
each corresponding to a row of the Pascal triangle.
The implementation avoids any arithmetic except addition.
<lang jq># pascal(n) for n>=0; pascal(0) emits an empty stream.
def pascal(n):
def _pascal: # input: the previous row
. as $in
| .,
if length >= n then empty
else
reduce range(0;length-1) as $i
([1]; . + [ $in[$i] + $in[$i + 1] ]) + [1] | _pascal
end;
if n <= 0 then empty else [1] | _pascal end ;</lang>
'''Example''':
pascal(5)
{{ Out }}
<lang sh>$ jq -c -n -f pascal_triangle.jq
[1]
[1,1]
[1,2,1]
[1,3,3,1]
[1,4,6,4,1]</lang>
 
=={{header|K}}==
2,497

edits