Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCF/LCM in Haskell

Tags:

haskell

I'm extremely new to Haskell.

Is there a simple way to find the GCF or LCM (Least Common Multiple) in Haskell?

like image 227
Dynamic Avatar asked Oct 27 '11 21:10

Dynamic


People also ask

When to use the LCM and GCF?

So will there ever be a time when we will need to use both the GCF, Greatest Common Factor and LCM, Least Common Multiple? Yes, whenever we perform operations with fractions ! For instance, we may need to use the LCM to help us add two fractions , and also the GCF to simplify our result .

How do you find the LCM and GCF of a cake?

To find the greatest common factor, you multiply all the numbers to the left of the cake. To find the LCM, I tell the students to draw a big “L” around the cake, and multiply all the numbers in the “L” together. Here is the example we did in class to find the GCF and LCM of 24 and 36.

How do you find LCM and GCF in purple math?

LCM and GCF. Purplemath. To find either the Least Common Multiple (LCM) or Greatest Common Factor (GCF) of two numbers, you always start out the same way: you find the prime factorizations of the two numbers.

How do you find LCMS and GCFs in Excel?

Note: There is another method for finding LCMs and GCFs; it's called the "listing" method. For instance, to find the LCM of 4and 6, you'd list their multiples, starting with the smallest and working your way up, until you found the first duplicate.


2 Answers

By GCF, do you mean the greatest common factor, or greatest common divisor? That's gcd, avaliable from the prelude, as is lcm, the least common multiple.

like image 190
Daniel Fischer Avatar answered Sep 24 '22 14:09

Daniel Fischer


I'm not sure what the LCF is but the GCF is a Haskell favorite. Using the Euclidean Algroithm you can really get a sense of how Haskell works. http://en.wikipedia.org/wiki/Euclidean_algorithm There is a great explanation of how the algorithm is set up here http://en.literateprograms.org/Euclidean_algorithm_(Haskell) .

This type of recursion is common in Haskell and it shows how expressive the language can be.

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

This uses pattern matching on the arguments to say the greatest common factor of any number and 0 is the first number. If the numbers are both non-zero, then look for the greatest common factor of the second and the first mod the second. Eventually this will reach 0 in the second argument. This will trigger the first pattern and return the first argument which is the answer.

[EDIT]

The function should actually be:

gcd a 0 = a
gcd a b = b `seq` gcd b (a `mod` b) where

This will force the evaluation of the previous recursion step's (a mod b) and will prevent a huge thunk being built in memory if, say, you are GCDing 1243235452 and 6095689787662. Seq forces the argument into its "Weak Head Normal Form" or evaluates the outer most data structure of the argument.

like image 34
Erik Hinton Avatar answered Sep 22 '22 14:09

Erik Hinton