# Basic primality test predicate in Prolog

Advertisements I am trying to create a predicate isPrime/1 that checks if a given number is prime or not. I have come up with the following code: primeRec(_, 2). primeRec(X, Y) :- Y > 2, X mod Y-1 > 0, primeRec(X, Y-1). isPrime(2). isPrime(X) :- X > 1, X mod 2 > 0, primeRec(X, X).… Read More Basic primality test predicate in Prolog

# What does the 'line' predicate mean in Prolog?

Advertisements I am currently trying to understand some Prolog code for a game, but I somehow can’t find any documentation about a specific predicate or rule that the programmer used, called ‘line’. He uses it in the argument list of other predicates, but neither is it customly defined by him nor can I find it… Read More What does the 'line' predicate mean in Prolog?

# Reverse the output of the Depth-First-Search Algorithm

Advertisements I want to reverse the output of the code. goal(hall). move(gateA,gateB). move(gateA,gateG). move(gateB,gateC). move(gateB,gateH). move(gateG,gateL). move(gateG,gateF). move(gateL,gateS). move(gateC,gateD). move(gateH,gateO). move(gateD,gateI). move(gateD,gateJ). move(gateI,gateP). move(gateP,gateQ). move(gateJ,gateR). move(gateR,hall). goSolveTheMaze( Start, Goal) :- depthfirst( [], Start, Goal). depthfirst( Path, Start, [Start | Path] ) :- goal( Start). depthfirst( Path,Start, Hall) :- move( Start, Node1), depthfirst( [Start | Path],… Read More Reverse the output of the Depth-First-Search Algorithm

# how do i flatten a complicate list in prolog?

Advertisements I have a list, which looks like -> [[[7, 8, 9], [7, 8, 9]]], I want to become follow example, [[[7,8,9],[7,8,9]]] -> [[7,8,9],[7,8,9]] is this possible? >Solution : It is possible: unpack([Ls], Ls). e.g. ?- unpack([[[7, 8, 9], [7, 8, 9]]], Answer). Answer = [[7, 8, 9], [7, 8, 9]]

# How to "return" two values in prolog

Advertisements I have two functions, first one calculates how many negative elements are in list, the second one forms list with indexes of negatives elements. I need to write a function called goal_negative_positions, that will "return" two values. My version don’t work, it always returns false. How to make it correct? negative_count([], 0):-!. negative_count([Head|Tail], Count):-… Read More How to "return" two values in prolog

# solving of equations by the Euler method in Prolog

Advertisements I wrote this mini pseudo code that solves the equation using the Euler method: // y’= x^2 – 5y int n = 10; // count of steps double h = 0.5; // step double x = 0; double y = 1; for (int i = 0; i < n; i++) { y += h… Read More solving of equations by the Euler method in Prolog