Greatest Common Divisor

From Mojo Puzzler Wiki
Jump to: navigation, search
Return to: Chialisp
Return to: Recursive Functions
(mod (num1 num2)
    (defun remainder (x y)
     (r (divmod x y)))
    (defun gcd (num1 num2)
      (if (= num2 0)
          num1
          (gcd num2 (remainder num1 num2))))
  (gcd num1 num2)
  )

Example Usage

brun '(a (q 2 4 (c 2 (c 5 (c 11 ())))) (c (q (a (i (= 11 ()) (q . 5) (q 2 4 (c 2 (c 11 (c (a 6 (c 2 (c 5 (c 11 ())))) ()))))) 1) 6 (divmod 5 11)) 1))' '(99 33)'

33

brun '(a (q 2 4 (c 2 (c 5 (c 11 ())))) (c (q (a (i (= 11 ()) (q . 5) (q 2 4 (c 2 (c 11 (c (a 6 (c 2 (c 5 (c 11 ())))) ()))))) 1) 6 (divmod 5 11)) 1))' '(206 40)'

2