#This section defines the sequence, if the number is even, divide by two. 
#If the number is odd, multiply the number by three and add one.
#NULL is to end sequence when s reaches 1.
sequence <- function(s)
{
  if(s == 1)
    return(NULL)
  
  if(s %% 2 == 0)
    return(s / 2)
  
  return(3 * s + 1)
}


#The below section calls the seqcuence section from above. This is called repeatedly
#Until 1 is returned. The sapply function creates a list from each iteration of the
#sequence.
nlist <-sapply(1:30, function(i)
{
  o <- i
  while(i != 1)
  {
    i <- sequence(i)
    o <- c(o, i)
  }
  return(o)
})

#Displays n lists
nlist


#This creates the Fibonacci number sequence. This method was used as you can 
#change the lenghth of the sequence if desired
len <- 10
fibvals <- numeric(len)
fibvals[1] <- 1
fibvals[2] <- 1
for (i in 3:len) { 
  fibvals[i] <- fibvals[i-1]+fibvals[i-2]
} 


#create the vector n (1:30) and identify whether the values are a Fibonacci number
n<-c(1:30)

fibonacci <- n %in% fibvals


#identify whether the square root of the numbers 1:30 are even numbers
st<- (sqrt(n) - floor(sqrt(n)))==0


#Displays if value is a square root
st


#calculate the length of each of the sequences held within nlist
terms<-sapply(nlist,length)


#create a dataframe to hold the data
df1<- data.frame("N" = n,"Terms" = terms,"Fibonacci" = fibonacci,"Integer sqrt" = st)

#displays the dataframe
df1


#Create a vector that contains n^2
n2<- n^2
n2



#Replace Fibonacci column with the new column and rename
df1[["Fibonacci"]]<-n2

colnames(df1)[colnames(df1) == "Fibonacci"]<-"N*N"


#Display the new dataframe
df1

