#Adam Smith - Student number 2317631 

options (scipen=999)                      #turn off scientific notation - AKA use full numbers / no rounding 

n<-200                                    #define how many iterations - AKA target number 

pell<-numeric(n)                          #create vector named pell to insert numbers into which is the length defined by our vector n
pell[1]<-0                                #assign the first number / iteration
pell[2]<-1                                #assign the second number / iteration
for (i in 3:n) {                          #create loop - starts at 3 position
  pell[i] <- 2 * pell[i-1] + pell[i-2]    #for every iteration double the previous iteration and add the iteration before it 
}                                         #close the loop

pelldata<-data.frame(pell)                #create a dataframe called pelldata which includes the created (pell) data 

Index_n<-1:200                            #create vector index_n which increases from 1:200


pelldata <- data.frame(                   #insert into dataframe called pelldata
  Index_n = 1:n,                          #column made from vector index_n which is 1 iteration to the predefined n iteration number
  Pell = pelldata                         #after the Index_n column insert pelldata
)                                         #close the rewrite of dataframe

fib<-numeric(n)                           #create a vector called fib which is numeric and length of vector (n)
fib[1]<-0                                 #assign the first number / iteration
fib[2]<-1                                 #assign the second number / iteration
for (i in 3:n){                           #loop starts from 3rd position
  fib[i] <- fib[i - 1]+fib[i - 2]         #for every 3rd iteration add the previous 2 iterations together
}                                         #close the loop

pelldata <- data.frame(                   #Insert into dataframe pelldata
  Index_n = 1:n,                          #Index_n 
  Fibonacci = fib,                        #column called Fibonacci created from the just created fib sequence
  Pell = pell                             #column called Pell created from the pell sequence
)                                         #close rewrite of dataframe

pell_ratio <- numeric(n)                            #create vector pell_ratio which is numeric 
for (i in 2:n){                                     #start loop from second iteration
  pell_ratio[i-1] <- (pell[i-1]+pell[i]) / pell[i]  #insert into pell_ratio first iteration and previous iteration divided by current iteration
}                                                   #close loop

pelldata <-data.frame(                    #insert into dataframe called pelldata
  index_n = 1:n,                          #Index_n column which increases from 1 to predetermined n limit
  Fibonacci = fib,                        #Fibonacci column generated from fib sequence created 
  Pell = pell,                            #Pell column generated from pell sequence created
  pell_ratio = pell_ratio                 #pell_ratio column generated from pell_ratio sequence created
)                                         #close rewrite of dataframe

SR2 <- sqrt(2)                                        #Create a vector called SR2 which contains the square root of 2
dec_match <- numeric(n)                           #Create a vector called dec_match which is numeric and the length of vector (n) 

for (i in 1:nrow(pelldata)) {                         #Start a loop which runs through every iteration of data frame
  counter <- 0                                        #Create a vector counter to count how many matches - start counter at 0
  for (d in 1:20) {                                   #Create a loop checking upto 20 decimal places (d)
    PR_rounded <- round(pelldata$pell_ratio[i], d)    #round pell_ratio to a maximum of 20 decimal places (d)
    SR2_rounded <- round(SR2, d)                      #round square root of 2 (SR2) to 20 decimal places so both match (d) 
    
    if (identical(PR_rounded, SR2_rounded)) {         #identical used to check if the 2 vectors match   
      counter <- d                                    #if vectors match, update counter with d
    } else {                                          #if dont match, exit loop - checking for missmatch, as we want them to be the same.
      break                                           #exit loop
    }
  }
  dec_match[i] <- counter                         #store counter in decimal match for that iteration
}

pelldata$accuracy <- dec_match                    #insert the counter (dec_match) into the data frame. As this is added to the end as the end column no need to rewrite to specify order.
