Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Greatest common divisor of three numbers in Haskell

So there is gcd for two numbers, which i know how do define.

gcd a 0 = a

gcd a b = gcd b (a `mod` b)

gcd a b c = gcd3 

How could i define a gcd3 for finding the greatest divisor for three numbers. The definition of gcd3 is:

gcd3 :: Int -> Int -> Int -> Int

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can take the gcd of three numbers the same way you take the sum of three numbers: by doing it two at a time.

gcd3 :: Int -> Int -> Int -> Int
gcd3 x y z = (x `gcd` y) `gcd` z
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading