t-sky
6/17/2012 - 3:52 AM

one_divided_by_nine.pro

%% answer1 and answer returns a fraction ?????/????? that has ten digits different from each other,
%% and reduced to 1/9.

%% takeout/3 removes X from the list [X|R].
takeout(X,[X|R],R).
takeout(X,[F|R],[F|S]) :- takeout(X,R,S).

%% perm/2 generates permutations as W, from [X|R].
perm([],[]).
perm([X|R],W) :- perm(R,S), takeout(X,W,S).

%% simple solution
answer1(Numer, Denom) :-
  perm([0,1,2,3,4,5,6,7,8,9],[A,B,C,D,E,F,G,H,I,J]),
  Numer is A*10000+B*1000+C*100+D*10+E,
  Denom is F*10000+G*1000+H*100+I*10+J,
  Numer * 9 =:= Denom.

%% some restriction added.
answer(Numer, Denom) :- % Numer:分子, Denom:分母
  perm([0,1,2,3,4,5,6,7,8,9],[A,B,C,D,E,F,G,H,I,J]),
  %% 分子/分母 = 1/9 だから分母は9の倍数
  %% 分子+分母は9の倍数なので、分子は9の倍数
  %% 分子の一の位は5ではない。なぜなら9倍した結果、一の位が再び5になるため。
  E \== 5,
  J \== 5,
  (A+B+C+D+E) mod 9 =:= 0,
  (F+G+H+I+J) mod 9 =:= 0,
  Numer is A*10000+B*1000+C*100+D*10+E,
  Numer mod 9 =:= 0,
  Denom is F*10000+G*1000+H*100+I*10+J,
  Numer * 9 =:= Denom.