[erlang-questions] Sunday puzzle - trying to improve my Erlang

Matthias Lang matthias@REDACTED
Mon Jul 7 00:25:22 CEST 2008


On Saturday, July 05, Toby Thain wrote:

> I stumbled on this simple 'puzzle'[0] posted some years ago on  
> comp.lang.tcl (originally to an Icon mailing list).

After reading the description four times, I still wasn't completely
sure what 'uniquely determines' means, but ploughed ahead, assuming
it'd become obvious when I saw the solutions, which it did.

I wrote the solution below before looking at the other solutions, but
was coward enough to peek at Richard's much shorter one before posting.
The obvious advantage of my solution is that I understand it ;-)

Matt

--------------------

-module(sq).
-export([go/0]).

go() ->
    All = all_answers(),
    Unique_viers = [ Vier || {Vier, _} <- All, 
			     length([X || {X, _} <- All, X == Vier]) == 1],
    Unique_neuns = [ Neun || {_, Neun} <- All, 
			     length([X || {_, X} <- All, X == Neun]) == 1],
    [ {Vier, Neun} || {Vier, Neun} <- All, 
		      lists:member(Vier, Unique_viers), 
		      lists:member(Neun, Unique_neuns)].

all_answers() ->
    [ {{V,I,E1,R}, {N,E2,U,N}} || 
	{V,I,E1,R} <- possible_viers(),
	{N,E2,U,N} <- possible_neuns(), 
	E1 == E2, all_unique([V,I,R,N,U])].

square_digits() ->
    Squares = [ X * X || X <- lists:seq(30, 100)],
    [ {X div 1000, (X div 100) rem 10, (X div 10) rem 10, X rem 10} || X <- Squares].

possible_neuns() ->
    [ X || X = {N,E,U,N} <- square_digits(), all_unique([N,E,U])].

possible_viers() ->
    [ X || X = {V,I,E,R} <- square_digits(), all_unique([V,I,E,R])].

all_unique([]) -> true;
all_unique([H|T]) -> not lists:member(H, T) andalso all_unique(T).



More information about the erlang-questions mailing list