Quantcast
Channel: Yet Another Math Programming Consultant
Viewing all articles
Browse latest Browse all 805

Self-descriptive number

$
0
0

In http://stackoverflow.com/questions/35485185/puzzle-taken-from-gardner a small problem is given:

image

This is called a self-descriptive number.

Of course we can use a MIP solver instead of Prolog:

numbers

The solution looks ok:

---- VAR n  digit i

              LOWER          LEVEL          UPPER         MARGINAL

digit0        -INF            6.0000        +INF             .         
digit1        -INF            2.0000        +INF             .         
digit2        -INF            1.0000        +INF             .         
digit3        -INF             .            +INF             .         
digit4        -INF             .            +INF             .         
digit5        -INF             .            +INF             .         
digit6        -INF            1.0000        +INF             .         
digit7        -INF             .            +INF             .         
digit8        -INF             .            +INF             .         
digit9        -INF             .            +INF             .         

Very short Prolog code as mentioned in the post is:

:- use_module(library(clpfd)).

ten_cells(Ls) :-
numlist(0, 9, Nums),
pairs_keys_values(Pairs, Nums, Ls),
global_cardinality(Ls, Pairs).

It is extremely short but not exactly very readable or intuitive (I guess that was not one of the design criteria for Prolog). I prefer here the mathematical optimization model. A plus for the Prolog version is that is also verifies there is only a single solution for n=10.


Viewing all articles
Browse latest Browse all 805

Trending Articles