簡體   English   中英

Rstudio:使用 for 循環求解方程

[英]Rstudio: Solving an equation using a for loop

等式:1 + 2/3 + (2/3 * 4/5) + (2/3 * 4/5 * 6/7) +...+ (2/3 * 4/5 ... 38/39 )

一直在兜圈子,試圖在 Rstudio 中求解這個方程,如果有人能幫助我或指出我求解這個方程的方向,我將不勝感激。 謝謝:)

語言:R(基礎包); 接口:RStudio

您可以使用seq function 來構建您的分數,然后使用prod來獲取每個序列的產品。

##define first element as 1
res=1

##for loop to get fractions and calculate product
for(i in 1:19){
  res=c(res,prod(seq(2,i*2,2)/seq(3,i*2+1,2)))
}

##sum all elements
sum(res)

[1] 6.976346

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM