/* Euler #33 in Picat. """ 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. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go => time(euler33). euler33 => S = 1, foreach(Y in 1..9,Z in Y..9) X = 9.0*Y*Z/(10.0*Y-Z), if 1.0*floor(X)==X*1.0, Y/Z < 1.0, X < 10.0 then S := (S*Y)/Z % , % ,println([x=X,y=Y, z=Z, (floor(10*Y+X)),"/", (floor(Z+10*X)), "s*y"=(S*Y), Y,"/",Z, floor(1/S)]) end end, println(floor(1/S)). %% %% 0.001s %% euler33b :- check(1,9,1,S), SInv is round(1/S), writeln(SInv). check(Y1,Y2,S,S) :- Y1 >= Y2. check(Y1,Y2,S0,S) :- Y1 < Y2, check(Y1,Y2,1,9,S0,ZS), Y3 is Y1+1, check(Y3,Y2,ZS,S). check(_Y1,_Y2,Z1,Z2,S,S) :- Z1 > Z2. check(Y1,Y2,Z1,Z2,S0,S) :- Z1 =< Z2, Tmp is 10.0*Y1-Z1, Tmp != 0.0, X is 9.0*Y1*Z1/(10.0*Y1-Z1), ( (1.0*floor(X)=:=X*1.0, Y1/Z1 < 1.0, X < 10.0 ) -> S1 is (S0*Y1)/Z1 ; S1 is S0 ), Z3 is Z1 + 1, check(Y1,Y2,Z3,Z2,S1,S).