Pascal's triangle: Difference between revisions
Content added Content deleted
m (added highlighting.) |
|||
Line 3,506: | Line 3,506: | ||
1 5 10 10 5 1 |
1 5 10 10 5 1 |
||
1 6 15 20 15 6 1</pre> |
1 6 15 20 15 6 1</pre> |
||
====Recursive==== |
|||
<lang javascript> |
|||
const aux = n => { |
|||
if(n <= 1) return [1] |
|||
const result = Array.from({ length: n }) |
|||
const prevLayer = aux(n - 1) |
|||
const shifted = [0, ...prevLayer] |
|||
return shifted.map((x, i) => (prevLayer[i] || 0) + x) |
|||
} |
|||
const pascal = n => { |
|||
for(let i = 1; i <= n; i++) { |
|||
console.log(aux(i).join(' ')) |
|||
} |
|||
} |
|||
pascal(8) |
|||
</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 |
|||
</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |