I am doing project euler question 33 and have divised a refactor to solve it but I can’t think of how to remove the digit if it is the same across both x and y.
I got this far:
import Ratio
import List
p33 = [ (x%y) | y <- [10..99] , x <- [10..y], (x `rem` 10) /= 0 , (y `rem` 10) /= 0 , x /= y , (length $ nub $ concat $ map decToList [x,y]) == 3 , [numerator(x%y),denominator(x%y)] == WHAT GOES HERE? ]
The cancelling of 0’s is not allowed.
What it should do is:
49/98 {cancel the 9's}
to get:
4/8 as the result. But I can’t think of how to remove the common digits from each number.
Let
xandybe the two numbers. Then one can remove the digits inxwhich it has in common withylike this:Flip
xandyto remove digits fromy. This usesxsData.List.(\\)ys, which removes the first occurrence of each element inysfromxs.Edit: you may have solved problem 33 by now. If not, let me give you some more hints:
The code which I gave above, i.e.
read (show x \\ show y)is not really suitable for this problem. What happens ifxequals ab andyequals ba, for some digits a and b?The solution to this problem can be written as a single list comprehension. You may want to use
Data.List.intersectandData.List.nubto create the following term in your list comprehension:Now
sranges over the digits thatxandyhave in common. You can remove these fromxandyusingI cannot be more explicit without solving the exercise for you, but I’ll leave you with two more hints:
y <- [10..99], x <- [10..y], x /= y. Observe that this can be written more succinctly asy <- [10..99], x <- [10..y-1].Data.Ratio, which provides an easy way to test the equality of rational numbers and automatically calculates the numerator and denominator of a fraction in its reduced form.