#test squence for fib and pell found on github

#fib squence and integer
#fn <- 200        # number of terms
#fa <- 0          # first term
#fb <- 1          # second term

#cat(fa, fb, " ")  # print first two terms

#for (i in 3:fn) {
#  fib <- fa + fb
#  cat(fib, " ") # print current term
#  fa <- fb
#  fb <- fib
#}

#pnum <- 200
#pa <- 0
#pb <- 1

#cat(pa, pb, " ")

#pell squence 
#for (i in 3:pnum){
#  pell <- 2 * pb + pa
#  cat(pell, " ")
#  pa <- pb
#  pb <- pell
#}




n<-200 #defines numeric 

#defaults vectors to 0
fs<-numeric(n)
ps<-numeric(n)
napx<-numeric(n)
dif<-numeric(n)
accurate<-numeric(n)

#fib start and vectors
a<-0 
b<-1
fs<-c(a,b) 

for (i in 3:n) {
  fn<-a+b #caculates the fib equation
  fs<-c(fs,round(fn, 1))
  a<-b
  b<-fn
}


#pell start and vectors
x<-0
y<-1
ps<-c(x,y)

for (i in 3:n){
  pn<-2*y+x #calculates the pell equation 
  ps<-c(ps,round(pn, 1))
  x<-y 
  y<-pn
}

#nth approximate equation calculation
for (i in 3:n){ 
  napx[i]<-(ps[i-1]+ps[i])/ps[i]
}
#cbind(d,napx)


#difference calculating with the sqaure root 2
sq2 <- sqrt(2) #define sqaure root
#calculate the difference
for (i in 2:n) {
  dif[i]<-round(abs(napx[i]-sq2),5) #abs allows positive numbers
}

#calculating accuracy within the decimals places. Used github and AI to research
for (i in 2:n){
apxv<-napx[i] #approx value is defined in the nth value 
tval<-sq2
e<-0 
f<-1 

for (t in 1:15){
  apxd<-floor(apxv*f) %% 10 #floor returns largest number when rounded
  tdig<-floor(tval*f) %% 10
  if (!is.na(apxd) && !is.na(tdig) && apxd == tdig) { #used ai to enchance and clear errors. is.na checks missing values in df
    e<-e+1 #checks how many matches 
  } else {
    break
  }
  f<-f*10 #returns the scale back
}
accurate[i]<-e
}

#creating the dataframe defining the columns
d<-data.frame(Term=1:n,fibnumber=fs,pellnumber=ps,nthsapproximate=napx,difference=dif,accuracy=accurate,stringsAsFactors=FALSE)

