
# Assignment 1 - Due date: 17/01/20
# Lecturer: Dr Nik Whitehead
# Module: Statistical Programming
# University of Wales Trinity St David

# Student No: 1707800
# Student Name: Mike Wakelyn






# Task 2 - Wondrous





# (1) Collatz Conjecture

#Define range of n for use throghout script. 
n<-1:30



# Collatz Conjecture - Start

# Function to test logic of collatz conjecture
numseq <- function(num)
{
  # If num = 1, do nothing
  if(num == 1)
    return(NULL)
  
  # If num is even, return num divided by 2
  if(num %% 2 == 0)
    return(num / 2)
  
  # If neither of the above conditions is met, num is odd. Return num * 3 + 1
  return(3 * num + 1)
}


# Function to output the values of the collatz conjecture for n in 1:30
# sapply() turns output in to a list

# Apply the below function to n (n<- 1:30)
colcon<-(sapply(n, function(input)
{
  result <- input
  # While input not equal to 1 and input in n, loop through the function
  while(input != 1)
  {
    
    input <- numseq(input)
    result <- c(result, input)
    
  }
  return(result)
}))


#Display the list of collatz conjecture values for each value of n
colcon
# Get the lengths of each list item and store in its own vector for use with data frame
lengthcolcon<-lengths(colcon)
# Display vectore containing lengths of colcon 
lengthcolcon








# (2) Is n a fibonacci number

# Create first 2 numbers of Fibonacci sequence
fib<-c(1,1)

# Fib loop
for (i in 2:29) {
  # add the value of fib[i]+fib[i-1] to the end of the fib vector
  fib<-c(fib,fib[i]+fib[i-1])
}


#define empty logical vector to store results of the below 'for' loop for use in data frame
n_is_fib<-vector(mode="logical", length=30)

  # n is fibonacci number loop - iterate through values of n
  for (i in seq(n)){
  # If value of n[i] is in fib, n_is_fib[i] becomes "TRUE"
    if (n[i] %in% fib){
      n_is_fib[i]<-"TRUE"
    }
  # Else - If value of n[i] is NOT in fib, n_is_fib[i] becomes "FALSE"
    else {
      n_is_fib[i]<-"FALSE"
    }
  }

# Display n_is_fib
n_is_fib





# (3) Has n an integer square root

#define empty logical vector to store results of the below 'for' loop for use in data frame
n_is_sqrt<-vector(mode="logical", length=30)

# n integer square root loop - iterate through values of n
for (i in seq(n)){
  # If value of n[i] has an integer square root, n_is_sqrt[i] becomes "TRUE"
    if (sqrt(n[i])==(round(sqrt(n[i])))){
      n_is_sqrt[i]<-"TRUE"
    }
  # Else - If value of n[i] does NOT have an integer square root, n_is_sqrt[i] becomes "FALSE"
    else {
      n_is_sqrt[i]<-"FALSE"
    }
}

# Display n_is_sqrt
n_is_sqrt





# (4) Data Frames

# Define data frame from vectors created above.  n=1:30, length of colcon list items, n is a fibonacci number and n is a square integer
ccsummary<-data.frame(n, lengthcolcon, n_is_fib, n_is_sqrt, stringsAsFactors = FALSE)
colnames(ccsummary)<- c("n", "Terms", "Fibonacci", "Integer Sqrt")
# Display data frame
ccsummary





# (5) Data Frames cont...

# Replace "Fibonacci" column with "N*N" column containing the values of n^2 (n*n)
ccsummary<-cbind(ccsummary[1:2], "N*N"=(n^2), ccsummary[4])
# Display changed data frame
ccsummary