## 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.

## 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:

Reference: Prolog Programming for Artificial Intelligence (4th Edition) (International Computer Science Series)  (A great book!)

## 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 […]

## 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 […]

## 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 […]

## 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 […]

## 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]). […]