#Common Variable Declaration for All Task
#=========================================
  
#maxNumber is a variable here which contains value of 200(Maximum Value).
maxNumber <- 200 

#took a variable named fiboNumber, pellNumber for giving place of Fibonacci Number,Pell Number
fiboNumber <- numeric(maxNumber) 
pellNumber <- numeric(maxNumber)

#seted a value for fiboNumber, pellNumber to index 1 & 2 = 0 & 1
fiboNumber[1:2] <- c(0,1)
pellNumber[1:2] <- c(0,1)

#made index column
n <- 0:(maxNumber-1)


#Conditional Part for each Task
#-------------------------------

#**Problem-1:**
#===============

#started condition with while loop(easier than if/else or for loop)
#started index from 3 for condition because already I have declared index 1 & 2 = 0 & 1.
i <- 3
 while (i <= maxNumber) {
  fiboNumber[i] <- fiboNumber[i-1] + fiboNumber[i-2]
  pellNumber[i] <- 2 * pellNumber[i-1] + pellNumber[i-2]
#increased 1 after every loop, It will help to go next index.
  i <- i+1 
 }

#**Making Data table(data.frame) for combine the value**
pellData <- data.frame(
  SlNo = n,
  Fibonacci = fiboNumber,
  Pell= pellNumber
)



#**Problem-2:**
#===============

#an empty vector(numeric) to store approximations
approxNum <- numeric(maxNumber)
approxNum[1] <- NA

# Applied the given formula using while loop
i <- 2
while (i <= maxNumber) {
  approxNum[i] <- (pellNumber[i - 1] + pellNumber[i]) / pellNumber[i] 
  i <- i + 1 
}

pellData$Approximation <- approxNum


#**Problem-3:**
#===============

# Pre-allocate a numeric vector to store the 200 difference values.
diffVector <- numeric(maxNumber)

# Calculate the precise value of sqrt(2) once
sqrt2Value <- sqrt(2)

# Use a FOR loop to iterate through every row (i) from 1 to 200.
# The FOR loop structure is cleaner for fixed-length iteration than a WHILE loop.
for (i in 1:maxNumber) {
  
# Check if the Approximation value is missing (NA), which should only be 
# the first term (i=1) since P(n-1) is undefined for n=0.
  
  if (is.na(pellData$Approximation[i])) {
    
# If the Approximation is NA, the Difference must also be NA.
    diffVector[i] <- NA
    
  } else {
    
# Otherwise, perform the calculation: (Approximation - True_Value)
    difference <- pellData$Approximation[i] - sqrt2Value
    
# Round the raw difference to the required five decimal places.
    diffVector[i] <- round(difference, 5)
    
  }
}

# Add the newly calculated vector as the 'Difference' column to pellData.
pellData$Difference <- diffVector


#**Problem-4:**
#===============

# Stored the 200 accuracy values to numeric vector & Calculate the precise value again of sqrt(2).
accuracyVector <- numeric(maxNumber)
sqrR2Value <- sqrt(2)

# Used FOR loop to repeat from 1-200 value.
for (i in 1:maxNumber) {
  
# Checked the Approximation value by using If/Else conditions.
# If the Approximation is NA, the Accuracy must also be NA.
  
    if (is.na(pellData$Approximation[i])) {
    accuracyVector[i] <- NA
  } else {
    
#Calculated the absolute difference between the approximation and the real value.
    absDifference <- abs(pellData$Approximation[i] - sqrR2Value)
    
# Applied the accuracy formula: -log10(absDifference)
    logAccuracy <- -log10(absDifference)
    
# Rounded the largest integer number 
    accuracyVector[i] <- floor(logAccuracy)
    
  }
}
# Added the calculated vector as the 'Accuracy' column to pellData.
pellData$Accuracy <- accuracyVector

#  Final Result & Printed DataFrame
# ==================================

head(pellData) #print the value(first 6 values)

