Question

In a beginner course, we have been asked to do some classic cryptograms in Prolog. They all work, except this one, which just goes on and on without finding any solution, except trying to make my PC blow up by overheating:

FORTY + TEN + TEN = SIXTY

crypto2(F,O,R,T,Y,E,N,S,I,X) :-
  digit(F), digit(O), digit(R), digit(T), digit(Y), digit(E), digit(N), digit(S), digit(I), digit(X),
  F =\= O, F =\= R, F =\= T, F =\= Y, F =\= E, F =\= N, F =\= S, F =\= I, F =\= X,
  O =\= R, O =\= T, O =\= Y, O =\= E, O =\= N, O =\= S, O =\= I, O =\= X,
  R =\= T, R =\= Y, R =\= E, R =\= N, R =\= S, R =\= I, R =\= X,
  T =\= Y, T =\= E, T =\= N, T =\= S, T =\= I, T =\= X,
  Y =\= E, Y =\= N, Y =\= S, Y =\= I, Y =\= X,
  E =\= N, E =\= S, E =\= I, E =\= X,
  N =\= S, N =\= I, N =\= X,
  S =\= I, S =\= X,
  I =\= X,
  10000 * F + 1000 * O + 100 * R + 210 * T + Y + 20 * E + 2 * N =:= 10000 * S + 1000 * I + 100 * X + 10 * T + Y.

All the digit to digit inequalities have been written with this bit of python, to preclude human error:

nbs = ["F", "O", "R", "T", "Y", "E", "N", "S", "I", "X"]
for i in range(len(nbs)):
  s= ""
  for j in range(i + 1, len(nbs)):
    s += nbs[i] + " =\= " + nbs[j] + ", "
  print s

Not written here is a series of ten simple facts (digit(0). to digit(9).) asserting that 0-9 are digits.

Can someone please spot a glaring mistake, or does it really take more than ten minutes, whereas simpler six-digit cryptograms solve immediately?

Thanks!

Was it helpful?

Solution

you can fasten things a bit moving constraints 'first possible':

crypto2(F,O,R,T,Y,E,N,S,I,X) :-
  digit(F),
  digit(O), F =\= O,
  digit(R), F =\= R, O =\= R,
  digit(T), F =\= T, ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top