September 2010
M T W T F S S
« Dec    
 12345
6789101112
13141516171819
20212223242526
27282930  

Categories

Project Euler Problem 1 in Prolog

It is very hard to come up with Prolog answers to Project Euler. If you have any please add as a comment to problems posted here.

?View Code PROLOG1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
gen(X,Y,[],B):-
X >= Y.
 
gen(X,Y,[Head|Tail],B):-
Y>X,
Head is X,
X1 is X + B,
gen(X1,Y,Tail,B).
 
 
sum(X,Y,Sum):-
gen(X,Y,List1,3),
[...]

Displaying Trees in Prolog

Here we give out a procedure where the tree is rotated to the left so that the root becomes the left-most element and the leaves are moved to the right:

?View Code PROLOG1
2
3
4
5
6
7
8
9
10
show(Tree):-
 show2(Tree,0).
 
show2(nil,_).
 
show2(t(Left,X,Right),Indent):-
 Ind2 is Indent + 2,
 show2(Right,Ind2),
 tab(Indent),write(X),nl,
 show2(Left,Ind2).

Binary Trees in Prolog

A way to represent a tree in Prolog could be:
t(t(nil,1,nil), 2, t(t(nil,3,nil),4,nil))

However, a binary tree is the one that is ordered from left to right accoriding to a relation. The general method for searching in the binary dictionary is:
-if X is the root of D then X has been found, otherwise
-if X is less than [...]

Sorting Lists in Prolog

A list can be sorted if there is an ordering relation in between the terms. For example, here is an ordering relation:
gt(X,Y):- X > Y.
or for alphabetical order:
gt(X,Y):- X@>Y.

First we examine bubblesort. To sort a list:
- Find two adjacent elements, X and Y,in list such that gt(X,Y) is satisfied and swap X and Y in [...]

A Cryptarithmetic Puzzle in Prolog

Note: This example is taken from Prolog: Programming for Artificial Intelligence by Ivan Bratko

An example of a cryptarithmetic puzzle is

   DONALD

   GERALD

+________

   ROBERT

Note that all letters have to be assigned different digits.  We need to define a relationship sum(N1,N2,N) where N1,N2, and N represent the three numbers that give the solution to the puzzle. However, [...]

Eight Queens Problem in Prolog

The aim of this problem is to place eight queens on the empty chessboard in such a way that no queen attacks any other one. There will be three distinct solutions given.

Solution 1

At first, we need to define a representation of the board. Each square of chessboard can be represented by a pair of X and [...]

List Operations in Prolog

[ a, b, c, d] is a list in Prolog. The first item is the head of the list. The remaining part is called the tail.

.(Head, Tail) is the special functor to create lists. For example

.(a, .(b, .(c, .( d, [])))) creates the list above. Let’s observe some operations on lists.

member(X, [X|Tail]).
member(X,[Head|Tail]) :-
 member(X, Tail).

defines the [...]

Prolog Programming Language

Prolog is a programming language that bases on some mechanisms such as pattern matching, tree-based data structuring and automatic backtracking. It actually stands for programming in logic. Such an idea emerged in the early 1970s, to use logic as a programming language.  Prolog is especially a language for symbolic, non-numeric computatin. It is well suited [...]