September 2, 2018

Just Juxt #19: Greatest Common Divisor (4clojure #66)

GCD

Given two integers, write a function which returns the greatest common divisor.

(ns live.test
  (:require [cljs.test :refer-macros [deftest is testing run-tests]]))

(defn gcd [a b]
  (let [[x y] (sort [a b])
        [q r] ((juxt quot rem) y x)]
    (if (zero? r) x (recur x r))))

(deftest test-66
  (is (= (gcd 2 4) 2))
  (is (= (gcd 10 5) 5))
  (is (= (gcd 5 7) 1))
  (is (= (gcd 1023 858) 33)))
  
(run-tests)
Tags: coding exercises KLIPSE 4clojure Cryogen juxt