library(gmp)

#######################
#Prints out the next 1000 numbers and their power info
#In one of 3 ways: All numbers, All Odd, Odd 7MOD(8)
#Works up to 2^750 numbers
#######################

# Function to read a large integer input with commas
read_large_integer <- function(prompt) {
  as.bigz(gsub(",", "", readline(prompt = prompt)))
}

# Function to calculate the MOD Power structure
calculate_mod_power <- function(num) {
  TN <- num
  MD <- as.bigz(2)
  MDD1 <- TN %% MD
  SD <- 0
  
  while (SD != 1) {
    MDD2 <- MDD1
    MD <- MD * 2
    MDD1 <- TN %% MD
    if (MDD1 == MDD2 && MDD1 != 0) {
      SD <- 1
    }
  }
  R <- TN %% MD
  AP <- (num - R) / MD
  RR <- (MD * AP) + R
  list(MD = MD, AP = AP, R = R, RR = RR)
}

# Function to print all numbers
print_all_numbers <- function(x) {
  for (i in 0:999) {
    current_num <- x + i
    mod_power <- calculate_mod_power(current_num)
    mod8 <- current_num %% 8
    print(paste(
      as.character(current_num),
      "MOD Power:", as.character(mod_power$MD), "*", as.character(mod_power$AP), "+", as.character(mod_power$R), "=", as.character(mod_power$RR),
      "MOD8:", as.character(mod8)
    ))
  }
}

# Function to print all odd numbers
print_odd_numbers <- function(x) {
  count <- 0
  while (count < 1000) {
    if (x %% 2 != 0) { # Check if the number is odd
      mod_power <- calculate_mod_power(x)
      mod8 <- x %% 8
      print(paste(
        as.character(x),
        "MOD Power:", as.character(mod_power$MD), "*", as.character(mod_power$AP), "+", as.character(mod_power$R), "=", as.character(mod_power$RR),
        "MOD8:", as.character(mod8)
      ))
      count <- count + 1
    }
    x <- x + 1
  }
}

# Function to print numbers that are 7MOD(8)
print_mod7_numbers <- function(x) {
  count <- 0
  while (count < 1000) {
    if (x %% 8 == 7) { # Check if the number is 7MOD(8)
      mod_power <- calculate_mod_power(x)
      mod8 <- x %% 8
      print(paste(
        as.character(x),
        "MOD Power:", as.character(mod_power$MD), "*", as.character(mod_power$AP), "+", as.character(mod_power$R), "=", as.character(mod_power$RR),
        "MOD8:", as.character(mod8)
      ))
      count <- count + 1
    }
    x <- x + 1
  }
}

# Main function to get user input and execute the chosen option
main <- function() {
  cat("Choose an option:\n")
  cat("1. Print all numbers\n")
  cat("2. Print all odd numbers\n")
  cat("3. Print numbers that are 7MOD(8)\n")
  choice <- as.integer(readline(prompt = "Enter your choice (1, 2, or 3): "))
  
  if (choice %in% 1:3) {
    x <- read_large_integer("Enter a starting number: ")
    
    if (choice == 1) {
      print_all_numbers(x)
    } else if (choice == 2) {
      print_odd_numbers(x)
    } else if (choice == 3) {
      print_mod7_numbers(x)
    }
  } else {
    cat("Invalid choice. Please run the script again and enter a valid option.\n")
  }
}

# Call the main function to execute the code
main()
