    # Assignment PELL.R
  
# Set the length of Fibonacci sequence you want
sequence_length <- 200
# Initialize the vector to store Fibonacci numbers
fib <- numeric(sequence_length)
# First two Fibonacci numbers
fib[1] <- 1
fib[2] <- 1
# Generate Fibonacci numbers
for (i in 3:sequence_length) {
  fib[i] <- fib[i-1] + fib[i-2]
}
# Print the Fibonacci numbers
print(fib)

# Function to calculate Pell numbers
calculate_pell <- function(n) {
  pell <- numeric(n)
  pell[1] <- 0
  pell[2] <- 1
  if (n > 2) {
    for (i in 3:n) {
      pell[i] <- 2 * pell[i - 1] + pell[i - 2]
    }
  }
  
  return(pell)
}

# Generate Pell numbers for the first 200 terms
n <- 200
pell_numbers <- calculate_pell(n)
print(pell_numbers)

# Create data frame pelldata
pelldata <- data.frame(n = 1:200,Fn = fib, Pn = pell_numbers)

# Creating new column Approx to calculate approximation for pell numbers
pelldata$Approx <- (pelldata$Pn - 1 + pelldata$Pn) / pelldata$Pn

# Creating new column Diff to calculate difference between the approximation using sqrt function
pelldata$Diff <- pelldata$Approx - sqrt(2)

# Calculate accuracy for each approximation
pelldata$Accuracy <- NA
for (i in 1:nrow(pelldata)) {
    approx <- pelldata$Approx[i]
    decimal_places <- 0
  while (round(approx, decimal_places) != round(sqrt(2),
                                                decimal_places)) {
    decimal_places <- decimal_places + 1
  }
# Assign the number of decimal places to Accuracy column
  pelldata$Accuracy[i] <- decimal_places
}
print(pelldata)
                                      # Script made by Osvaldas Stanelis 1802408
