# the size that each vector is going to be
index <- 200

### Task 1 Q1 #############################################################################
# create starting vectors  
pellArr <- c(0,1)
fibArr <- c(0,1)
indexArr <- 1:index

# append new values to vector
for (i in indexArr) {
  # calculates the next pell number using 2*P(n-1) + p(n-2)
  pellNum <- (2*pellArr[i]) + pellArr[i-1]
  # adds pellNum to the vector
  pellArr <- c(pellArr, pellNum) 
  
  # calculates the next Fibonacci number using f(n-1) + f(n-2)
  fibNum <- fibArr[i] + fibArr[i-1]
  # adds fibNum to the vector
  fibArr <- c(fibArr, fibNum) 
}

# add vectors to a dataFrame and set vectors to a fixed length
pellData <- data.frame (
  Index = indexArr,
  Fibonacci = fibArr[1:index], # using [1:index] to fixed the length
  Pell = pellArr[1:index]      # before it goes into the dataframe
)



### Task 1 Q2 #############################################################################
# create starting vector
approxArr <- NULL

# append new values to vector
for (i in seq(2,index+1)) {
  # calculates the approximation of root 2 using the formula (p(n-1) + p(n))/p(n)
  approxNum <- (pellArr[i] + pellArr[i-1])/pellArr[i]
  # adds approxNum to the vector
  approxArr <- c(approxArr, approxNum) 
}

# add vectors to the dataFrame
pellData <- cbind(pellData, Approximation = approxArr)


### Task 1 Q3 #############################################################################
# create starting vector
# diffArr <- NULL

# append new values to vector
# for (i in pellData[["Approximation"]]) {
  # diffNum <- sqrt(2)-i
  
  # rounds the difference to 5 decimal places
  # diffNum <- round(diffNum, digits = 5)
  # diffArr <- c(diffArr, diffNum)
}

# add vectors to the dataFrame and set vectors to a fixed length
# using [1:index] to fixed the length before it goes into the dataframe
# pellData <- cbind(pellData, Difference = diffArr[1:index])


# calculate the difference then rounds it to 5dp then adds it to the dataFrame
pellData <- cbind(pellData, Difference = round(sqrt(2)-pellData$Approximation, 5))



### Task 1 Q4 #############################################################################
# creates and format starting vectors
accuracyArr <- NULL
sqrt2 <- sqrt(2)
# create new variable only containing the decimal places
sqrt_char <- substr(sqrt2,3,nchar(sqrt2))

# loops through each approximate value
for (i in pellData[["Approximation"]]) {
  
  # create new variable only containing the decimal places
  appr_char <- substr(i,3,nchar(i))

  # count through values representing how many decimal points are accurate
  for (y in seq(1000)) {
  
    # splice character to level of decimal points
    sqrt2_char <- substr(sqrt_char, 1, y)
    appr2_char <- substr(appr_char, 1, y)
    
    # first failed statement, get (y-1) as decimal point accuracy and appended to vector
    if (sqrt2_char!=appr2_char) {
      accuracyArr <- c(accuracyArr, y-1) # adds (y-1) to the vector
      break
    }
  }
}

# add vector to dataframe and sets it to a fixed length
pellData <- cbind(pellData, Accuracy = accuracyArr[1:index])



print(pellData)
