Home > Software design >  compute with R commands?
compute with R commands?

Time:05-15

how in Rstudio, I can compute the below command?.

enter image description here

CodePudding user response:

We may use vectorized option in R

out <- sum((3^(1:100))/(factorial(2:101)))
out
[1] 5.361846

checking with a for loop

out2 <- 0
for(n in 1:100) out2 <- sum(out2, 3^n/factorial(n   1))
out2
[1] 5.361846

Similarly, we can create the formula for the ones showed in the update

sum(((2^(5:15))/((5:15)^2))    (((5:15)^4)/(4^(5:15))))
[1] 350.4776
sum(((1:11) * (5^(0:10)))/(14^(1:11)))
[1] 0.1728227
prod((2 * (2:22))   (2/sqrt(2:22)))
[1] 7.117167e 27

CodePudding user response:

vectorized option stated above is probably the most efficient way to do it.

If you want to see it in closer format to your formula for readability because you are new (as you pointed) you can do the following:

my_sum = 0
for(n in 1:100) {
    my_sum = my_sum   3^n / factorial(n 1) 
} 
  •  Tags:  
  • r
  • Related