Dichotomy Sequence plot and csv file generator

dichotomy_sequence <- function(n_terms) {
  # Ensure the number of terms is positive
  if (n_terms <= 0) {
    return(NULL)
  }
  
  # Generate the sequence
  sequence <- 1 / (2^(1:n_terms))
  
  return(sequence)
}

write_to_csv <- function(sequence, filename) {
  data <- data.frame(Term = 1:length(sequence), Value = sequence)
  write.csv(data, file = filename, row.names = FALSE)
}

plot_sequence <- function(sequence) {
  barplot(sequence, main = "Dichotomy Paradox Sequence", 
          xlab = "Term", ylab = "Value", border = "blue", col = "lightgray")
}

# write to csv and plot sequence
sequence <- dichotomy_sequence(10) # change value to increase or decrease sequence (33 was used for Kaggle upload)
write_to_csv(sequence, "dichotomy_sequence.csv")
plot_sequence(sequence)