

#
# Task 1
#


# Create a column of index, 1 to 200
index <- 1:200

# Calculated Fibonacci numbers from n=1 to n=200
# Initializes the sequence: F1=0, F2=1.
fibonacci <- c(0,1)

for (n in 1:198){ # Loop for the remaining 198 terms (n=3 to 200).
  
  # Appends the sum of the last two elements (Fn = Fn-1 + Fn-2) to the vector fibonacci. 
  fibonacci <- c(fibonacci,fibonacci[length(fibonacci)]+fibonacci[length(fibonacci)-1])
}

# Calculated Pell numbers from n=1 to n=200
pell <- c(0,1) # Initializes the sequence: P1=0, P2=1.

for (n in 1:198) {# Loop for the remaining 198 terms.
  
  # Appends the next Pell number (Pn = 2*Pn-1 + Pn-2) to the vector pell.
  pell <- c(pell,2*pell[length(pell)]+pell[length(pell)-1])
}

# Defind pelldata dataframe
# Creates the base data frame with index, Fibonacci numbers, and Pell numbers.
pelldata <- data.frame(index_n = index, Fn = fibonacci, Pn = pell)


#
# Task 2
#


# Calculated sqrt2 approximation using the Pell ratio (Pn-1 + Pn) / Pn

sqrt2_approxi <- c(0) # Initializes with 0 placeholder for n=1.

for (n in 2:200){ # Loop starts from n=2.
  # Calculates the approximation (P[n-1] + P[n]) / P[n] and appends it.
  # pelldata[n,3] refers to Pn. pelldata[n-1,3] refers to Pn-1.
  sqrt2_approxi <- c(sqrt2_approxi,(pelldata[n-1,3]+pelldata[n,3])/pelldata[n,3])
}

pelldata$pell_sqrt2 <- sqrt2_approxi # Adds the approximations as a new column to the data frame. 


#
# Task 3
#


# Calculates the absolute difference
differs <- c() # Initializes the difference vector.

for (n in 1:200){
  
  # Calculates the absolute difference between the approximation (column 4) and sqrt(2), rounded to 5 decimal places.
  differs <- c(differs, round(abs(pelldata[n,4]-sqrt(2)),5))
}

pelldata$differs <- differs # Adds the difference vector to the data frame.


#
# Task 4 
#


# Calculates the approximation accuracy (consecutive correct decimal places)
accuracy <- c() # Initializes the accuracy vector.

standard <- format(sqrt(2), scientific = FALSE, nsmall = 15) # Formats true sqrt(2) into a high-precision string (at least 15 decimals).

dec_sqrt2 <- strsplit(standard, "\\.")[[1]][2] # Extracts the decimal part of the true sqrt(2) string.

for (n in 1:200){ # Outer loop: iterates through each Pell approximation.
  same_num <- 0 # Resets the counter for correct digits for each approximation.
  
  # Formats the current Pell approximation into a comparable string.
  str_pell <- format(pelldata[n,4], scientific = FALSE, nsmall = 15)
  
  # Extracts the decimal part of the current Pell approximation.
  dec_pell_sqrt2 <- strsplit(str_pell,"\\.")[[1]][2]
  
  # The loop runs up to the minimum length of the two decimal strings.
  for (location in 1:min(nchar(dec_sqrt2),nchar(dec_pell_sqrt2))){
    
    # Compares the digit at the current location.
    if (substring(dec_pell_sqrt2,location,location) == substring(dec_sqrt2,location,location)) {
      same_num <- same_num + 1 # Increment if the digits match.
    }
    else{
      break # Stop immediately if a mismatch is found.
    }
  }
  accuracy <- c(accuracy,same_num) # Appends the final count of consecutive correct digits.
}

pelldata$accuracy <- accuracy # Adds the accuracy vector to the data frame.

print(pelldata) # Prints the final data frame to the console.
