[R] Pascal's Triangle
Spencer Graves
spencer.graves at structuremonitoring.com
Wed Dec 28 11:10:37 CET 2011
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:
> 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 ----
>> Date: Wed, 28 Dec 2011 03:42:11 -0600 (CST)
>> From: r-help-bounces at r-project.org (on behalf of "Jason J.
> Pitt"<pittjj at uchicago.edu>)
>> Subject: Re: [R] Pascal's Triangle
>> To: "Matilda E. Gogos"<matildaelizabethv at gmail.com>, r-
> help at r-project.org
>> 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 ----
>>> Date: Tue, 27 Dec 2011 23:14:27 -0500
>>> From: r-help-bounces at r-project.org (on behalf of "Matilda E.
>> Gogos"<matildaelizabethv at gmail.com>)
>>> Subject: [R] Pascal's Triangle
>>> To: r-help at r-project.org
>>>
>>> Hello,
>>>
>>> Looking to generate Pascal's triangle in R. How do I get
>> started? Thanks,
>>> --
>>> Matilda Gogos
>>> matildaelizabethv at gmail.com
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-
>> project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible
>> code.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-
> project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible
> code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Spencer Graves, PE, PhD
President and Chief Technology Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph: 408-655-4567
web: www.structuremonitoring.com
More information about the R-help
mailing list