Learn R Programming

numbers (version 0.8-5)

Pascal triangle: Pascal Triangle

Description

Generates the Pascal triangle in rectangular form.

Usage

pascal_triangle(n)

Value

Returns the Pascal triangle as an (n+1)x(n+1) rectangle with zeros filled in.

Arguments

n

integer number

Details

Pascal numbers will be generated with the usual recursion formula and stored in a rectangular scheme.

For n>50 integer overflow would happen, so use the arbitrary precision version gmp::chooseZ(n, 0:n) instead for calculating binomial numbers.

References

See Wolfram MathWorld or the Wikipedia.

Examples

Run this code
n <- 5; P <- pascal_triangle(n)
for (i in 1:(n+1)) {
    cat(P[i, 1:i], '\n')
}
## 1 
## 1 1 
## 1 2 1 
## 1 3 3 1 
## 1 4 6 4 1 
## 1 5 10 10 5 1 

if (FALSE) {
P <- pascal_triangle(50)
max(P[51, ])
## [1] 126410606437752
}

Run the code above in your browser using DataLab