Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
37
Task/Pascal-matrix-generation/R/pascal-matrix-generation-1.r
Normal file
37
Task/Pascal-matrix-generation/R/pascal-matrix-generation-1.r
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
lower.pascal <- function(n) {
|
||||
a <- matrix(0, n, n)
|
||||
a[, 1] <- 1
|
||||
if (n > 1) {
|
||||
for (i in 2:n) {
|
||||
j <- 2:i
|
||||
a[i, j] <- a[i - 1, j - 1] + a[i - 1, j]
|
||||
}
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
# Alternate version
|
||||
lower.pascal.alt <- function(n) {
|
||||
a <- matrix(0, n, n)
|
||||
a[, 1] <- 1
|
||||
if (n > 1) {
|
||||
for (j in 2:n) {
|
||||
i <- j:n
|
||||
a[i, j] <- cumsum(a[i - 1, j - 1])
|
||||
}
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
# While it's possible to modify lower.pascal to get the upper matrix,
|
||||
# here we simply transpose the lower one.
|
||||
upper.pascal <- function(n) t(lower.pascal(n))
|
||||
|
||||
symm.pascal <- function(n) {
|
||||
a <- matrix(0, n, n)
|
||||
a[, 1] <- 1
|
||||
for (i in 2:n) {
|
||||
a[, i] <- cumsum(a[, i - 1])
|
||||
}
|
||||
a
|
||||
}
|
||||
28
Task/Pascal-matrix-generation/R/pascal-matrix-generation-2.r
Normal file
28
Task/Pascal-matrix-generation/R/pascal-matrix-generation-2.r
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
> lower.pascal(5)
|
||||
[,1] [,2] [,3] [,4] [,5]
|
||||
[1,] 1 0 0 0 0
|
||||
[2,] 1 1 0 0 0
|
||||
[3,] 1 2 1 0 0
|
||||
[4,] 1 3 3 1 0
|
||||
[5,] 1 4 6 4 1
|
||||
> lower.pascal.alt(5)
|
||||
[,1] [,2] [,3] [,4] [,5]
|
||||
[1,] 1 0 0 0 0
|
||||
[2,] 1 1 0 0 0
|
||||
[3,] 1 2 1 0 0
|
||||
[4,] 1 3 3 1 0
|
||||
[5,] 1 4 6 4 1
|
||||
> upper.pascal(5)
|
||||
[,1] [,2] [,3] [,4] [,5]
|
||||
[1,] 1 1 1 1 1
|
||||
[2,] 0 1 2 3 4
|
||||
[3,] 0 0 1 3 6
|
||||
[4,] 0 0 0 1 4
|
||||
[5,] 0 0 0 0 1
|
||||
> symm.pascal(5)
|
||||
[,1] [,2] [,3] [,4] [,5]
|
||||
[1,] 1 1 1 1 1
|
||||
[2,] 1 2 3 4 5
|
||||
[3,] 1 3 6 10 15
|
||||
[4,] 1 4 10 20 35
|
||||
[5,] 1 5 15 35 70
|
||||
Loading…
Add table
Add a link
Reference in a new issue