I have to write R code for creating pascal triangle. The code should such that if I enter, eg, i=400 , it should give me a pascal triangle for that.
Can anyone help me?
I have to write R code for creating pascal triangle. The code should such that if I enter, eg, i=400 , it should give me a pascal triangle for that.
Can anyone help me?
This StackOverflow thread seems like it might be a useful fit!
thanks. can you also tell me, how to obtain row sum of each row in pascal triangle that is created. the sum should be 2^n.
From the stackoverflow thred @mara linked to:
pascalTriangle <- function(h) {
lapply(0:h, function(i) choose(i, 0:i))
}
The run:
p = pascalTriangle(4)
unlist(lapply(p, sum))
#[1] 1 2 4 8 16
and compare with
2**(0:4)
# [1] 1 2 4 8 16
Just a friendly reminder about community.rstudio.com's homework policy: FAQ: Homework Policy
TL:DR How to Ask a Homework Related Question:
- Do not ask verbatim copy-paste questions
- Explicitly mention the course you are taking.
- If reasonable, be sure to ask your question as a reproducible example (reprex). Preferably using the
reprex
-package