0

Here is a snippet of cryptarithmetic in prolog

sum1( [D1|N1], [D2|N2], [D|N], C1, C, Digs1, Digs)  :-
  sum1( N1, N2, N, C1, C2, Digs1, Digs2),
  digitsum( D1, D2, C2, D, C, Digs2, Digs).

As below explains

sum1( N1, N2, N, C1, C, Digits1, Digits)
where N1, N2 and N are our three numbers, 
      C1 is carry from the right, and
      C is carry to the left (after the summation).
      Digits1 is the list of available digits for instantiating the
      variables in N1, N2, and N.
      Digits is the list of digits that were not used in the
      instantiation of these variables.

I really don't get it what does C1 mean in sum1( [D1|N1], [D2|N2], [D|N], C1, C, Digs1, Digs), for C2 stands for carry form right, C stands for carry to the left, then what does C1 stand for?

false
  • 10,264
  • 13
  • 101
  • 209
stonestrong
  • 337
  • 1
  • 5
  • 13

1 Answers1

1

C,C1,C2 can only assume 0,1 values. Arithmetic rules require that C1 will be 0 in first call, and C will be 0 on last call. C2 become C on recursive call, then it propagates the carry after the sum. C1 it's the carry added to N1,N2 to get N.

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • nice! why not simplify it as `sum1( [D1|N1], [D2|N2], [D|N], C, Digs1, Digs) :- sum1( N1, N2, N, C2, Digs1, Digs2), digitsum( D1, D2, C2, D, C, Digs2, Digs).` for C1 never alter in the recursion call and always be zero? – stonestrong May 27 '13 at 07:29
  • oh! it really can be simplified and testing succeeds, but we should also change base case. in `Bratko's book`, `sum(N1, N2, N) :-sum1( N1, N2, N, 0, 0, [0,1,2,3,4,5,6,7,8,9], _).`, and base case is `sum1( [], [], [], C, C, Digits, Digits).`. Now I change them to `sum(N1, N2, N) :-sum1( N1, N2, N, 0, [0,1,2,3,4,5,6,7,8,9], _).`, base case to `sum1( [], [], [], 0, Digits, Digits).` – stonestrong May 27 '13 at 07:43
  • You're right. I think it could be constrained *after* all the recursive steps, but seems to be useless to the purpose. – CapelliC May 27 '13 at 07:45