Difference between revisions of "Pascal's triangle"

From Fōrmulæ wiki
Jump to: navigation, search
(Creation of page)
 
(Description (from Rosetta Code))
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
=== Description (from Rosetta Code) ===
 
=== Description (from Rosetta Code) ===
  
[http://en.wikipedia.org/wiki/Pascal%27s_triangle Pascal's triangle] is an arithmetic and geometric figure often associated with the name of [http://en.wikipedia.org/wiki/Blaise_Pascal Blaise Pascal], but also studied centuries earlier in India, Persia, China and elsewhere.
+
{| class="wikitable" style="width:100%;"
 +
| [http://en.wikipedia.org/wiki/Pascal%27s_triangle Pascal's triangle] is an arithmetic and geometric figure often associated with the name of [http://en.wikipedia.org/wiki/Blaise_Pascal Blaise Pascal], but also studied centuries earlier in India, Persia, China and elsewhere.
  
 
Its first few rows look like this:
 
Its first few rows look like this:
Line 38: Line 39:
 
See also
 
See also
 
* [https://rosettacode.org/wiki/Evaluate_binomial_coefficients Evaluate binomial coefficients]
 
* [https://rosettacode.org/wiki/Evaluate_binomial_coefficients Evaluate binomial coefficients]
 +
|}
  
 
=== Program ===
 
=== Program ===
  
[[File:PascalTriangleFlowchart.png|thumb|link=http://dictionary.formulae.org/mediawiki/images/5/54/AnonymousRecursionProgramFlowchart.png|The same program when the flowchart package visualization is selected. Click/tap to enlarge]]
+
[[File:PascalTriangleFlowchart.png|thumb|link=http://wiki.formulae.org/mediawiki/images/4/4e/PascalTriangleFlowchart.png|The same program when the flowchart package visualization is selected. Click/tap to enlarge]]
  
 
[[File:PascalTriangleCode.png|border]]
 
[[File:PascalTriangleCode.png|border]]

Latest revision as of 17:11, 23 March 2019

This page is the answer to the task Pascal's triangle in the Rosetta Code.

Description (from Rosetta Code)

Pascal's triangle is an arithmetic and geometric figure often associated with the name of Blaise Pascal, but also studied centuries earlier in India, Persia, China and elsewhere.

Its first few rows look like this:

    1
   1 1
  1 2 1
 1 3 3 1

where each element of each row is either 1 or the sum of the two elements right above it.

For example, the next row of the triangle would be:

1 (since the first element of each row doesn't have two elements above it)
4 (1 + 3)
6 (3 + 3)
4 (3 + 1)
1 (since the last element of each row doesn't have two elements above it)

So the triangle now looks like this:

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Each row n (starting with row 0 at the top) shows the coefficients of the binomial expansion of (x + y)n.

Task

Write a function that prints out the first n rows of the triangle (with f(1) yielding the row consisting of only the element 1).

This can be done either by summing elements from the previous rows or using a binary coefficient or combination function.

Behavior for n ≤ 0 does not need to be uniform, but should be noted.

See also

Program

The same program when the flowchart package visualization is selected. Click/tap to enlarge

PascalTriangleCode.png

Example

PascalTriangleOutput.png