Simple for loop in R producing “replacement has length zero” in R

前端 未结 1 496
夕颜
夕颜 2020-12-19 03:36

I am a novice in R, and so what seemed to work fine in C and Python, surprisingly breaks down in R. I am trying to calculate the product of the first 1000 Fibonacci numbers.

相关标签:
1条回答
  • 2020-12-19 04:28

    Arrays in R are 1-based.

    Fibonacci[1]<-1
    Fibonacci[2]<-1
    Product<-1
    for (i in 3:1000)
    {
    

    (the remainder as in your question)

    The problem is Fibonacci[0] which is a 0-length numeric. When i = 2, this expression has a right hand side of numeric(0):

    Fibonacci[i]<-(Fibonacci[i-1])+(Fibonacci[i-2])
    
    0 讨论(0)
提交回复
热议问题