assignment on fibbonacci series

##### Title

##### Question

on giving the following input

fibbonacci <- function(t1,t2,t3...........tn){

tn <- 0

t1=0

t2=1

t3=t1+t2

for(n in t1:tn){

t(n)=t(n-2)+t(n-1)

}

return(tn)

}

fibbonacci(t1,t2,t3.........tn)

the following comments appear-

fibbonacci <- function(t1,t2,t3...........tn){

+ tn <- 0

+ t1=0

+ t2=1

+ t3=t1+t2

+ for(n in t1:tn){

+ t(n)=t(n-2)+t(n-1)

+ }

+ return(tn)

+ }

> fibbonacci(t1,t2,t3.........tn)

Error in t(n) <- t(n - 2) + t(n - 1) : could not find function "t<-"

>

R Functions-in-R 11-12 min 20-30 sec

__Answers:__

In your case, you have not defined what

**t**means. It would be best if you defined a function first. For instance, you can write the code as given below:t1 = 0

t2 = 1

count = 2

print(t1)

n <- as.integer(readline("Enter the value of n: "))

if(n == 1){

print(t1)

} else{

print(t1)

print(t2)

while(count < n){

nth = t1 + t2

print(nth)

t1 = t2

t2 = nth

count = count + 1

}

}

The above-mentioned code is taken from https://www.datamentor.io/r-programming/examples/fibonacci-sequence/

Login to add comment