Skip to content

Pascal's Triangle

5 messages · Matilda E. Gogos, Jason J. Pitt, Ingmar Visser +1 more

#
Hi Matilda,

My ignorance of the mathematical properties of Pascal's 
Triangle might make me overlook a much easier way to do this, 
but my first brute force thought was to use a list to store 
each level of the triangle (i.e. a list of vectors). You can 
then use the values of the previous level to calculate the 
values of the next level.

Then it should just be a matter of formatting using spaces or 
tabs and printing each level (vector) in the list. There may 
be some nuances to this depending how you want it to look, but 
nothing a little trial and error and Googling can't solve.

HTH,

Jason

---- Original message ----
Gogos" <matildaelizabethv at gmail.com>)
started? Thanks,
project.org/posting-guide.html
code.
#
Oh and also, if it makes sense, you can use n choose k ( 
choose() ) to calculate each element of the triangle if you 
don't want to deal with indicies, addition, etc.

Jason

---- Original message ----
Pitt" <pittjj at uchicago.edu>)
help at r-project.org
but
project.org/posting-guide.html
code.
#
n <- 5
PascalsTriangle <- vector('list', n)
PascalsTriangle[[1]] <- 1
for(i in 2:n){
     x0 <- PascalsTriangle[[i-1]]
     x01 <- c(0, x0)
     x10 <- c(x0, 0)
     PascalsTriangle[[i]] <- x01+x10
}


I hope I'm not doing a homework problem.


Spencer
On 12/28/2011 1:53 AM, Jason J. Pitt wrote: