# Cieran Hughes
# 1800730
# Computing

# initialize varables
nth<-c(1:30) # starting terms

# varables for dataframe
nterm<-NULL
lterm<-NULL
Fibonacci<-NULL
IntegerSqrt<-NULL
NtimesN<-NULL

# fibonacci function
Fib_Sequance <- function(nthterm) {
  
  # initalise varables
  fibNumber1<-(0)
  fibNumber2<-(1)
  fibSequance<-(0)
  fibTestNum<-NULL
  counter<-(1)
  
  # creates fibonacci sequance up to 30
  while (fibNumber2 < 30) {
    fibNumber1<- fibNumber1 + fibNumber2
    fibNumber2<- fibNumber1 + fibNumber2
    
    fibSequance<-c(fibSequance,fibNumber1,fibNumber2) # adds new numbers to sequance
  }
  
  # loop runs legnth of the fibonacci sequance
  for (c in seq(fibSequance)) {
    
    # tests if number passed in is in the sequance
    if (nthterm == fibSequance[c]) {
      # number in sequance
      fibTestNum<-(TRUE)
      break
    } else {
      # number not in sequance
      fibTestNum<-(FALSE)
    }
  }
  
  return(fibTestNum) # returns if in sequance
}

# loop all items in nth varable
# these items are the starting terms for each sequance
for (i in seq(nth)) {
  
  sequance<-c(nth[i]) # starting term in to sequance varable
  x<-c(1) # initialize couter
  
  # create sequance until last number is 1
  while (sequance[x] > 1) {
    
    if ((sequance[x]%%2) == 0) {
      # number is even
      sequance<-c(sequance,sequance[x]/2) # calcuate new value and add to end of sequance
    } 
    else { 
      # number is odd
      sequance<-c(sequance,(sequance[x]*3)+1) # calcuate new value and add to end of sequance
    }
    
    x = x+1 # increase counter by 1
  }
  
  # populating dataframe vectors
  nterm<-c(nterm,nth[i]) # pass in starting term
  lterm<-c(lterm,length(sequance)) # pass in sequance legnth
  Fibonacci<-c(Fibonacci,Fib_Sequance(nth[i])) # pass in if part of fibonacci sequance
  IntegerSqrt<-c(IntegerSqrt,(sqrt(nth[i])%%1)==0) # pass in if had an integer square root
  NtimesN<-c(NtimesN, (sqrt(nth[i]))) # pass in n times n
}

# create dataframe
dataframe<-data.frame(nterm, lterm, Fibonacci, IntegerSqrt, stringsAsFactors = FALSE)

# rename dataframe collunm
names(dataframe)[1] <- "N"
names(dataframe)[2] <- "Terms"
names(dataframe)[4] <- "Integer sqrt"

# disaply dataframe
dataframe

# replace column in dataframe 
dataframe$Fibonacci<-(NtimesN)

# rename dataframe collunm
names(dataframe)[3] <- "N*N"

# display updated dataframe
dataframe