[net.lang.prolog] Solution to Truthteller Puzzle

Moss.UPenn%Rand-Relay@sri-unix.UUCP (09/30/83)

From:  Chris Moss <Moss.UPenn@Rand-Relay>

The solution published in the Digest of 21 Sep from K. Handa
works, but it seems like an Algol program recoded in Prolog.

Here's a solution which uses no asserts or retracts, and it
runs about 100 times faster ( extrapolated timings for 10
digits in Unh Prolog as I only did 8 digits for Handa's
program ).


/* Find a number of n digits of which the first is the
   number of 0's in the number, the second the number
   of 1's etc. */

go(N) :- guess(N, N, 0, 0, [], L), print(L), nl, fail.
go(N) :- statistics.

guess(1, Total, S, SS, L, T.L) :- !, T is Total-S,
        count(0, T.L, T).
guess(Val, Total, S, SS, L, List) :- V1 is Val-1,
        n(A),
        SS2 is V1*A+SS, SS2 =< Total,
        S2 is S+A,
        guess(V1, Total, S2, SS2, A.L, List),
        count(V1, List, A).

count(N, [], 0).
count(N, N.A, B) :- !, B>0, C is B-1, count(N,A,C).
count(N, A.B, C) :- count(N,B,C).

n(0). n(1). n(2). n(3). n(4). n(5). n(6). n(7). n(8). n(9).