How to resolve the algorithm Greatest common divisor step by step in the Clojure programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Greatest common divisor step by step in the Clojure programming language
Table of Contents
Problem Statement
Find the greatest common divisor (GCD) of two integers.
Greatest common divisor is also known as greatest common factor (gcf) and greatest common measure.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Greatest common divisor step by step in the Clojure programming language
Source code in the clojure programming language
(defn gcd
"(gcd a b) computes the greatest common divisor of a and b."
[a b]
(if (zero? b)
a
(recur b (mod a b))))
(defn gcd*
"greatest common divisor of a list of numbers"
[& lst]
(reduce gcd
lst))
(defn stein-gcd [a b]
(cond
(zero? a) b
(zero? b) a
(and (even? a) (even? b)) (* 2 (stein-gcd (unsigned-bit-shift-right a 1) (unsigned-bit-shift-right b 1)))
(and (even? a) (odd? b)) (recur (unsigned-bit-shift-right a 1) b)
(and (odd? a) (even? b)) (recur a (unsigned-bit-shift-right b 1))
(and (odd? a) (odd? b)) (recur (unsigned-bit-shift-right (Math/abs (- a b)) 1) (min a b))))
You may also check:How to resolve the algorithm Heronian triangles step by step in the C# programming language
You may also check:How to resolve the algorithm Reduced row echelon form step by step in the PHP programming language
You may also check:How to resolve the algorithm Filter step by step in the Erlang programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the PL/I programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the Standard ML programming language