So did anyone at all look at this program? I will include it here again, in text

form. I am interested in comments, please. Feel free to email me directly at

Quote:

> I have written the following program, using the strawberry prolog

> compiler (lite version). Its goal is to solve number sequences, such as

> those given on math tests, mensa tests, and the like (I.E. 1, 2, 4, ?).

> I self-proclaim that I am definitely a rookie when it comes to Prolog,

> so I am interested in your comments, suggestions, and improvements. I

> think this program works reasonably well for small problems, however it

> takes exponentially longer time to solve larger ones. This is an

> unavoidable result of the algorithm that I am using, which is to make

> sub-lists by finding the sums, differences, products, and quotients of

> elements of the larger lists, and then look for identity lists (single

> element lists containing 1, 0, or -1).

> Anyhow, have a look, let me know what you think, and please feel free to

> criticize (or even rewrite) the code!

> Glenn Emelko

%*************************************

%

%

% Number Sequence Solver Version 1.1

%

% (C) 2000 Glenn A. Emelko

%

%

% This program tries to solve for a

% number at the end of a number sequence

%

%*************************************

%-------------------------------------

% is_sum(A,B,C) -- solve the equation C=A+B

% if any two variables are known.

is_sum(A,B,C) :-

number(A), number(B), C is A + B;

number(A), number(C), B is C - A;

number(B), number(C), A is C - B.

%-------------------------------------

% is_prod(A,B,C) -- solve the equation C=A*B

% if any two variables are known.

is_prod(A, B, C) :-

number(A), number(B), C is A * B;

number(A), number(C), A =\= 0, B is C / A;

number(B), number(C), B =\= 0, A is C / B.

%-------------------------------------

% is_diff(A,B,C) -- solve the equation C=A-B

% if any two variables are known.

is_diff(A,B,C) :-

number(A), number(B), C is A - B;

number(A), number(C), B is A - C;

number(B), number(C), A is B + C.

%-------------------------------------

% is_div(A,B,C) -- solve the equation C=A/B

% if any two variables are known.

is_div(A,B,C) :-

number(A), number(B), B =\= 0, C is A / B;

number(A), number(C), C =\= 0, B is A / C;

number(B), number(C), A is B * C.

%-------------------------------------

% first(L,F) -- F is the first element

% of list L

first([A|_],A).

%-------------------------------------

% solve_seq(I,O) -- given input list I,

% find a sub-list O which is a set of

% sums, products, differences, or quotients,

% predicted by recognizing sub-lists

% which ultimately boil down to one of the

% identity lists ([], [0], [1], and [-1]).

solve_seq([],[]) :- !.

solve_seq([_|[]],[0]).

solve_seq([_|[]],[1]).

solve_seq([_|[]],[-1]).

solve_seq([A|[]],[A]).

solve_seq([A|[B]],[C]) :-

number(C),

B is A + C, !.

solve_seq([A|[B]],[C]) :-

number(C),

B is A * C, !.

solve_seq([A|[B]],[C]) :-

number(C),

B is A - D, !.

solve_seq([A|[B]],[C]) :-

number(C),

C =\= 0,

B is A / C, !.

solve_seq([A|T],[D|R]) :-

solve_seq(T, R), first(T, N), is_sum(A, D, N), !;

solve_seq(T, R), first(T, N), is_prod(A, D, N), !;

solve_seq(T, R), first(T, N), is_diff(A, D, N), !;

solve_seq(T, R), first(T, N), is_div(A, D, N).

solve_seq([_|[_]],[_]).

%-------------------------------------

% solve(I, O) -- solve an input list

% with a missing last element, filling

% in the last element into the output

% list, by using solve_seq, allowing

% backtracking to find all possible lists.

solve([],[]) :- !.

solve([_], [0]) :- !.

solve([_], [1]) :- !.

solve([_], [-1]) :- !.

solve([A], [A]).

solve(A, B) :-

B is A,

solve_seq(A, D),

solve(D, E),

solve_seq(B, E),

valid(B).

%-------------------------------------

% best_solve(I, O) -- solve an input list

% with a missing last element, filling

% in the last element into the output

% list, by using solve_seq, preventing

% backtracking from finding all lists.

best_solve([],[]) :- !.

best_solve([_], [0]) :- !.

best_solve([_], [1]) :- !.

best_solve([_], [-1]) :- !.

best_solve([A], [A]).

best_solve(A, B) :-

B is A,

solve_seq(A, D),

!,

best_solve(D, E),

solve_seq(B, E),

valid(B).

%-------------------------------------

% valid(I) -- true if I is a list in which

% all elements are numbers

valid([]).

valid([A]) :-

!,

number(A).

valid([A|R]) :-

number(A),

!,

valid(R).

%-------------------------------------

% member(X,L) -- true if X is a member of

% list L

member(X,[X | L]).

member(X, [_ | L]) :-

member(X, L).

%-------------------------------------

% showsolutions(L) -- assuming L is a list

% of valid solutions, display all

% unique solutions by only showing those

% which are not members of the remainder

% of the list

showsolutions([]).

showsolutions([A|B]) :-

member(A,B),

showsolutions(B).

showsolutions([A|B]) :-

write(A),nl,

showsolutions(B).

%-------------------------------------

% so here's the goal -- C is the input, with

% a missing last element, first find all

% possible solutions, then find only the best

% one(s).

?-

C is [1, 2, 4, _],

write("All solution(s):"), nl,

!,

findall(D,solve(C, D),Dlist),

showsolutions(Dlist),

write("Best solution(s):"),nl,

findall(E,best_solve(C,E),Elist),

showsolutions(Elist).

%

% Easy, right?

%