# Removing scientific notations from all results in an R session

options(scipen = 999)





# Task One



# Creating the index vector from 1 - 200

n<-c(1:200)

# Displaying the index

print(n)

# Creating the Fibonacci Sequence vector, starting with 0 and 1

fib<-c(0,1)

# Creating a 'for loop' for 200 elements

for (i in 2:199) {
  
  # Calculating each fib and adding the value of fib[i]+fib[i-1] 
  # to append for each element
  
  fib<-c(fib,fib[i]+fib[i-1])
}


# Displaying the Fibonacci Sequence

print(fib)

# Creating the Pell Sequence vector, starting with 0 and 1

pell<-c(0,1)

# Creating a 'for loop' for 200 element

for (x in 2:199) {
  
  # Calculating each pell and adding the value of 2*pell[x]+pell[x-1] 
  # to append for each element
  
  pell<-c(pell,2*pell[x]+pell[x-1])
}

# Displaying the Pell Sequence

print(pell)

# Creating the 'pelldata' dataframe

pelldata<-data.frame(Index = n, Fibonacci_Sequence = fib, Pell_Sequence = pell, 
                     stringsAsFactors=FALSE)



# Task Two



# Creating the 'pell_approximation' vector for 200 elements

pell_approximations <- numeric(200)

# Creating a 'for loop' for 200 elements

for (k in 2:200) {
  
  # Calculating the nth approximation ((Pn-1 + Pn)/2) for each element
  
  pell_approximations[k] <- (pell[k - 1] + pell[k]) / 2
}

# Displaying the 'pell_approximations'

print(pell_approximations)

# Adding the 'Pell_Approximations' column to the 'pelldata' dataframe

pelldata<-cbind(pelldata,Pell_Approximations = pell_approximations)





# Task Three



# Creating the 'differences' vector for 200 elements

differences <- numeric(200)

# Calculating the differences between 'pell_approximations and sqrt(2) 
# for each value of n

differences <- abs(pell_approximations - sqrt(2))

# Displaying the 'differences' calculation

print(differences)

# Calculating the square root difference between 'pell_approximations' and 'differences'

differences2 <- pell_approximations - differences

# Displaying the square root Difference

print(differences2)

# Adding the 'Square Root Difference1' and 'Square Root Difference2' 
# to the data frame, then removing 'Square Root Difference1' and renaming it
# once calculation is correct

# pelldata<-cbind(pelldata, Square_Root_Difference1 = round(differences, 5))                        # Removed as calculation is correct

pelldata<-cbind(pelldata, Square_Root_Difference = round(differences2, 5))





# Task Four



# Creating the 'accuracy' vector for 200 elements

accuracy <- numeric(200)

# Calculating the accuracy of the approximation for each element

for (s in 1:200) {
  
  # Converting the difference to a character string 
  # and counting the number of decimal places
  
  accuracy[s] <- nchar(sub(".*\\.", "", as.character(differences[s])))
}

# Displaying the 'accuracy'

print(accuracy)

# Adding the 'Accuracy' column to the 'pelldata' data frame

pelldata<-cbind(pelldata,Pell_Accuracy = accuracy)
