R/euclidean.R

Defines functions euclidean

Documented in euclidean

#' Euclidean algorithm
#'
#' Retunrs the greatest common divisor between a and b.
#' 
#' @param b A numeric value not equal to zero.
#' @param a A numeric value not equal to zero.
#' 
#' @return The greatest common divisor between \code{a} and \code{b}.
#' 
#' @references https://en.wikipedia.org/wiki/Euclidean_algorithm
#' 
#' @examples  
#' euclidean(18,84)
#' euclidean(16,2120)
#' 
#' @export


euclidean<-function(a,b){
  stopifnot(is.numeric(a),is.numeric(b))
  stopifnot(length(a)==1,length(b)==1)
  while(a != 0){
    k<-a
    a<-b%%a
    b<-k
  }
  return(abs(b))
}
harjew/lab3G3 documentation built on Nov. 4, 2019, 1:27 p.m.