Project Euler Problem 33
Digit cancelling fractions
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
Link to original description
Source code examples on Github
Erlang version
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #!/usr/bin/env escript %% -*- erlang -*- %%! -smp enable -sname p33 % vim:syn=erlang -mode(compile). main(_) -> {N,M} = lists:foldl(fun({A,B}, {C,D})-> {A*C,B*D} end, {1,1}, [{X,Y}||X<-lists:seq(10,99), Y<-lists:seq(10,99), isPhr(X,Y), X < Y]), io:format("Answer ~p ~n", [ round(M / N) ]). isPhr(X,X) -> false; isPhr(X,_) when X rem 10 == 0 -> false; isPhr(_,Y) when Y rem 10 == 0 -> false; isPhr(X,Y) -> case sets:to_list(sets:intersection(sets:from_list(integer_to_list(X)), sets:from_list(integer_to_list(Y)))) of [] -> false; Inter -> XX = [X1 || X1<- integer_to_list(X), lists:member(X1,Inter) == false], YY = [Y1 || Y1<- integer_to_list(Y), lists:member(Y1,Inter) == false], case {XX,YY} of {"", _} -> false; {_, ""} -> false; {"0",_} -> false; {_,"0"} -> false; _ -> case X/Y == list_to_integer(XX)/list_to_integer(YY) of true -> true ;_ -> false end end end. |
Python version
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #!/usr/bin/python3 def isPhr(x, y): if x == y: return False if x % 10 == 0 and y % 10 == 0: return False inter = [i for i in str(y) if i in str(x)] if inter != []: x1 = "".join([i for i in str(x) if i not in inter]) y1 = "".join([i for i in str(y) if i not in inter]) if x1 == "" or y1 == "": return False if x1 == "0" or y1 == "0": return False if x / y == int(x1) / int(y1): return True else: return False else: return False am, bm = 1,1 for (a,b) in [(x,y) for x in range(10,100) for y in range(10,100) if x < y and isPhr(x,y)]: am *= a bm *= b print("Answer %s" % (bm // am)) |