---
title: "TASK-1"
author: "Nisarg Jani"
date: "2025-10-18"
output:
  pdf_document: default
  html_document: default
---

```{r}
#     Component-1 & Task-1
```
```{r}
# Q 1: Generate Pell numbers, Fibonacci numbers, and store in a dataframe

# Number of terms to generate
n_terms <- 200

# Initialize vectors
n <- 0:(n_terms - 1)
fib <- numeric(n_terms)
pell <- numeric(n_terms)

# Fibonacci sequence
fib[1] <- 0  # F0
fib[2] <- 1  # F1
for (i in 3:n_terms) {
  fib[i] <- fib[i - 1] + fib[i - 2]
}

# Pell sequence
pell[1] <- 0  # P0
pell[2] <- 1  # P1
for (i in 3:n_terms) {
  pell[i] <- 2 * pell[i - 1] + pell[i - 2]
}

# Create dataframe
pelldata <- data.frame(
  n = n,
  Fibonacci = fib,
  Pell = pell
)

# Print the first few rows to check
print(head(pelldata, 15))



```
```{r}
# Q 2 Calculate the approximation of sqrt(2) for each Pell number
# Formula: (P_{n-1} + P_n) / P_n

# Create a vector for the approximation
approximation <- numeric(n_terms)

# First term (n = 0) can be set to NA because P_{-1} doesn't exist
approximation[1] <- NA

# Calculate for n = 1 to n_terms - 1
for (i in 2:n_terms) {
  approximation[i] <- (pell[i - 1] + pell[i]) / pell[i]
}

# Add as a new column to the pelldata dataframe
pelldata$Sqrt2_Approx <- approximation

# View the first few rows
print(head(pelldata, 15))

# Optional: save updated dataframe
write.csv(pelldata, "pelldata_with_approx.csv", row.names = FALSE)

```

```{r}

# Q 3 Calculate the difference between Pell approximation and actual sqrt(2)

# Actual value of sqrt(2)
actual_sqrt2 <- sqrt(2)

# Calculate the difference and round to 5 decimal places
difference <- round(pelldata$Sqrt2_Approx - actual_sqrt2, 5)

# Add as a new column to the dataframe
pelldata$Diff_from_sqrt2 <- difference

# View the first few rows
print(head(pelldata, 15))

# Optional: save the updated dataframe
write.csv(pelldata, "pelldata_with_diff.csv", row.names = FALSE)

```

```{r}
# Q4 Calculate the accuracy of the approximation as the number of correct decimal places

# Create a numeric vector to store accuracy values
accuracy <- numeric(n_terms)

# Define the true value of sqrt(2)
true_val <- sqrt(2)

# Convert the true value to string with high precision
true_str <- formatC(true_val, digits = 15, format = "f")
true_chars <- strsplit(true_str, "")[[1]]

# Loop through each approximation value
for (i in 1:n_terms) {
  
  # If approximation is NA, store NA
  if (is.na(pelldata$Sqrt2_Approx[i])) {
    accuracy[i] <- NA
  } else {
    # Convert approximation to string with same precision
    approx_str <- formatC(pelldata$Sqrt2_Approx[i], digits = 15, format = "f")
    approx_chars <- strsplit(approx_str, "")[[1]]
    
    # Count matching decimal digits (skip '1.')
    matches <- 0
    for (j in 3:length(true_chars)) {
      if (j <= length(approx_chars) && approx_chars[j] == true_chars[j]) {
        matches <- matches + 1
      } else {
        break
      }
    }
    
    # Store number of matching decimal digits
    accuracy[i] <- matches
  }
}

# Add results as a new column in the dataframe
pelldata$Accuracy_DP <- accuracy

# Display first few rows for verification
print(head(pelldata, 15))

# Optional: save the updated dataframe
write.csv(pelldata, "pelldata_with_accuracy.csv", row.names = FALSE)


```

