기타/R

[R]피보나치 수열 만들기

stonesy 2021. 10. 1. 22:33
728x90

https://github.com/Choseoyoung/R_practice

 

GitHub - Choseoyoung/R_practice

Contribute to Choseoyoung/R_practice development by creating an account on GitHub.

github.com

##fibonacci(n)
#0,1,1,2,3,5,8,...

fibonacci<-function(n){
	if(n<1)return(NA)
    else if(n==1)return(0)
    else if(n==2)return(1)
    
    f<-seq(0,1)
    for(i in 3:n){
    	f<-c(f,f[i-2]+f[i-1])
    }
    return(f)
}
728x90