group8lab3/R/euclidean.R

#' Euclidean Algorithm
#'
#' Finds the greatest common divisor of two numbers
#' 
#' @param a A number.
#' @param b A number.
#' @return The greatest common divisor of \code{a} and \code{b}.
#' @references \url{https://en.wikipedia.org/wiki/Euclidean_algorithm}
#' @export


euclidean <- function(a,b){ # This function takes two numbers and finds the greatest common divisor
  stopifnot(is.numeric(c(a,b)), length(c(a,b))==2) # The first numeric value @param b The second numeric value
  
  a<-abs(a)
  b<-abs(b)
  
  if (b>a){ # Exchange the values of a and b if b is greater than a
    num1 <- a
    a <- b
    b <- num1
  }
  
  if (b==0){ # Return a if b is 0
    return(a)
  }
  
  while(b!=0){
    num2 <- b # Store the value of b
    b <- a%%b # Change the value of b to the remainder of a mod b
    a <- num2 # Change the value of a to b before the mod operation
  }
  return(a) #The greatest common divisor
}
bayubeta/group8lab3 documentation built on Nov. 3, 2019, 2:08 p.m.