Perhaps it would be easiest to have a look at some similar code. As requested, I won't give you the answer, but you should be able to use these ideas to do what you want.
Brute force
Here we're just going to use all the pairs of numbers between 1
and x
to test if we can make x
as the sum of two square numbers:
sumOfSquares :: Int -> [Int]
sumOfSquares x = [ (a,b) | a <- [1..x], b <- [a..x], a^2 + b^2 == x]
You call this like this:
ghci> asSumOfSquares 50
[(1,7),(5,5)]
because 50 = 1^2+7^2 and also 50 = 5^2 + 5^2.
You can think of sumOfSquares
as working by first taking an a
from the list [1..x]
of numbers between 1
and x
and then another between that and x
. It then checks a^2 + b^2 == x
. If that's True, it adds (a,b)
to the resulting list.
Generate and check
This time let's generate some single numbers then check whether they're a multiple of another. This will calculate the least common multiple (lcm). For example, the least common multiple of 15 and 12 is 60, because it's the first number that's in both the 15 and 12 times tables.
This function isn't of the type you want but it uses all the techniques you want.
lcm :: Int -> Int -> Int
lcm x y = head [x*a | a <- [1..], (x*a) `mod` y == 0]
You can call that like this:
ghci> lcm 12 15
60
This time the list of numbers [1..]
is (in principle) infinite; good job we're just picking the first one with head
!
(x*a) `mod` y == 0
does the checking to see whether the number x*a
is a multiple of y
(mod
gives the remainder after division). That's a key idea you should use.
Summary
Use a <- [1..end]
to generate numbers, test them with a True/False expression (i.e. a Bool
), perhaps using the mod
function.