List split in Prolog - prolog

Getting error in Prolog.
I want the 1st list to have positive numbers divisible by 2 and 2nd list to have negative number divisible by 3.
This is the code I've written so far but I'm getting the output as 'false'
split([],[],[]).
split([X|L],[X|L1],L2):- X >=0, X mod 2 =:= 0, split(L,L1,L2).
split([X|L],L1,[X|L2]):- X<0, X mod 3 =:= 0, split(L,L1,L2).
test case - split([1,-2,-3,4,-17,3],L1,L2).
output - false
Can someone please tell me where am I going wrong?

You did not specify a clause for values that are not positive and dividable by two, or negative and dividable by three. In that case you probably do not want to include it in any list, so:
split([],[],[]).
split([X|L],[X|L1],L2):-
X >= 0,
X mod 2 =:= 0,
split(L,L1,L2).
split([X|L],L1,[X|L2]):-
X < 0,
X mod 3 =:= 0,
split(L,L1,L2).
split([X|L], L1, L2) :-
\+ ((X >= 0, X mod 2 =:= 0); (X < 0, X mod 3 =:= 0)),
split(L, L1, L2).

Related

Basic primality test predicate in Prolog

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).
but it does not seem to work, and I have no idea why. I've found my code to follow the same idea as this one here, except that mine always returns false. for any ?- isPrime(X). with X bigger than 2, which obviously should not happen.
The problem is that you need to define another variable, say Y1, and unify it with the evaluation of Y-1, i.e., Y1 is Y - 1 and use Y1 instead of Y-1 in the second rule for primeRec/1. This because if you want to evaluate an arithmetic expression you need to use is.
primeRec(X, Y) :- Y > 2, Y1 is Y - 1, X mod Y1 > 0, primeRec(X, Y1).

Argument is not instantiated, need it to start at zero but also be able to change it

Whenever I run my code, I get an error that the arguments are not instantiated.
ads(X,Z):- mod(X,2) =:= 0, Z is Z+X.
ads(X,Z) :- mod(N,2) =\= 0,Z is Z.
sum_of_nums(0,0,0).
sum_of_nums(X,Y,Z) :- X=<Y, ad(X,Z), sum_of_nums(X+1,Y,Z).
I want to be able to add numbers from X to Y but only the even ones. Don't know why it doesn't work.
First, there are some tiny errors, your compiler should indicate, like the unnecessary singleton variables in the second clause. After repairing this, and replacing ads by ad we have:
ad(X,Z):- mod(X,2) =:= 0, Z is Z+X.
ad(X,Z) :- mod(X,2) =\= 0,Z is Z.
sum_of_nums(0,0,0).
sum_of_nums(X,Y,Z) :- X=<Y, ad(X,Z), sum_of_nums(X+1,Y,Z).
?- sum_of_nums(1,2,S).
error(instantiation_error,(is)/2).
To locate this error, I will insert some goals false such that the
resulting program still produces this instantiation error.
ad(X,Z):- mod(X,2) =:= 0, Z is Z+X, false.
ad(X,Z) :- false, mod(X,2) =\= 0,Z is Z.
sum_of_nums(0,0,0) :- false.
sum_of_nums(X,Y,Z) :- X=<Y, ad(X,Z), false, sum_of_nums(X+1,Y,Z).
?- sum_of_nums(1,2,S).
error(instantiation_error,(is)/2).
Therefore, you have an error in this part already.
It's the Z is Z+X. On the right hand side of (is)/2 you always
need to have variables that are instantiated (known). And Z is not
known.
Variables are a bit different in Prolog. You cannot reassign them.
And also, writing this in Prolog directly doesn't really show what the
language is good at.
sum_of(X, Y, 0) :-
X > Y.
sum_of(X1, Y, Z1) :-
X1 =< Y,
( X1 mod 2 =:= 0 -> S = X1 ; S = 0 ),
X2 is X1+1,
sum_of(X2, Y, Z2),
Z1 is Z2+S.
A more idiomatic way would be:
?- between(1,6,N).
N = 1
; N = 2
; N = 3
; N = 4
; N = 5
; N = 6.
?- between(1,6,N), N mod 2 =:= 0.
N = 2
; N = 4
; N = 6.
?- findall(N, ( between(1,6,N), N mod 2 =:= 0 ), Ns).
Ns = [2,4,6].
?- findall(N, ( between(1,6,N), N mod 2 =:= 0 ), Ns), sum_list(Ns,Sum).
Ns = [2,4,6], Sum = 12.
sum_of(X,Y,Sum) :-
findall(N, ( between(X,Y,N), N mod 2 =:= 0 ), Ns),
sum_list(Ns,Sum).

Prolog - print the sum of the EVEN numbers from 1 to 10

I am trying to print all the even numbers from 1 to 10 using Prolog, and here is what I have tried:
printn(10,0):- write(10),!.
printn(X,Sum):-
( X mod 2 =:= 0 -> Sum is X+Sum, Next is X+1, nl, printn(Next);
Next is X+1, printn(Next) ).
but it returns false.
You don't need to create the list with the numbers from the beginning, it is better to examine numbers once:
print(X,Y):-print_even(X,Y,0).
print_even(X, X, Sum):-
( X mod 2 =:= 0 -> Sum1 is X+Sum;
Sum1 = Sum
), print(Sum1).
print_even(X, Y, Sum):-
X<Y, Next is X+1,
( X mod 2 =:= 0 -> Sum1 is X+Sum, print_even(Next, Y, Sum1);
print_even(Next, Y, Sum)
).
Keep in mind that in Prolog Sum is Sum+1 always fails you need to use a new variable e.g Sum1.
Example:
?- print(1,10).
30
true ;
false.
The most useful way of obtaining Prolog output is to capture the solution in a variable, either individually through backtracking, or in a list. The idea of "printing", which carries over from using other languages allows for formatting, etc, but is not considered the best way to express a solution.
In Prolog, you want to express your problem as a relation. For example, we might say, even_with_max(X, Max) is true (or succeeds) if X is an even number less than or equal to Max. In Prolog, when reasoning with integers, the CLP(FD) library is what you want to use.
:- use_module(library(clpfd)).
even_up_to(X, Max) :-
X in 1..Max,
X mod 2 #= 0, % EDIT: as suggested by Taku
label([X]).
This will yield:
3 ?- even_up_to(X, 10).
X = 2 ;
X = 4 ;
X = 6 ;
X = 8 ;
X = 10.
If you then want to collect into a list, you can use: findall(X, even_up_to(X), Evens).
What error do you have? Here is my solution:
Create list [1...10]
Filter it, excluding odd numbers
Sum elements of the list
Code:
sumList([], 0).
sumList([Head|Tail], Sum) :-
sumList(Tail, Rest),
Sum is Head + Rest.
isOdd(X) :-
not((X mod 2) =:= 0).
sumOfEvenNums(A, B, Out) :-
numlist(A, B, Numbers),
exclude(isOdd, Numbers, Even_numbers),
sumList(Even_numbers, Out).
Now you can call sumOfEvenNums(1, 10, N)
In ECLiPSe, you can write with iterator:
sum_even(Sum):-
( for(I,1,10),fromto(0,In,Out,Sum)
do
(I mod 2 =:= 0 -> Out is In + I;Out is In)
)
With library(aggregate):
evens_upto(Sum) :-
aggregate(sum(E), (between(1, 10, E), E mod 2 =:= 0), Sum).
Thanks to #CapelliC for the inspiration.

Checking circular primes in Prolog

I am new to Prolog and am having a hard time figuring out what is wrong with my code.
I am trying to find circular prime numbers
So far I have this code:
isCircularPrime(N):-
prime(N),
numDigits(N, Y),
Y2 is Y-1,
rotate(N,Y,N2),
circularPrime(N2, Y2, Y2).
circularPrime(_, 0, _).
circularPrime(N, 1, _):-prime(N).
circularPrime(N, I, J):-
I > 1,
prime(N),
I2 is I-1,
rotate(N,J,N2),
circularPrime(N2,I2,J).
Where:
numDigits makes Y the number of digits in N.
rotate makes N2 a rotation of N (e.g. 12 -> 21).
prime is true if N is prime, false otherwise
Currently it recurses infinitely whenever a number is prime but not a circular prime (e.g. 19, 23, etc). If someone could explain what I'm doing wrong, I would really appreciate it.
Here is the other code in case anyone wants to test this for themselves:
prime(2).
prime(3).
prime(N):-
N>3,
N mod 2 =\= 0,
\+ divisible(N,3).
divisible(N,I):- N mod I =:= 0.
divisible(N,I):-
I*I < N,
I2 is I + 2,
divisible(N, I2).
numDigits(N, 1):-
N<10, N>0.
numDigits(N, X):-
N2 is N/10,
numDigits(N2, Y),
X is (Y+1).
rotate(N, L, R):-
LastDigit is N mod 10,
Base is div(N, 10),
Exponent is L - 1,
Num is 10**Exponent,
NewBase is LastDigit*Num,
R is Base + NewBase.
I think the problem is in your numDigits/2 predicate.
It offers all kinds of answers, so the program keeps backtracking again and again:
numDigits(1, X).
X = 1 ;
X = 2 ;
X = 3 ;
X = 4 ;
X = 5 etc...
After adding cuts to numDigits/2 the programs seems to behave correctly:
isCircularPrime(19).
false.

OR condition in Prolog

I want to check whether a number is divisible by one of 3, 5, or 7.
How could I write a rule something like -
isDivide(N) :- N mod 3 =:= 0 OR N mod 5 =:= 0 OR N mod 7 =:= 0.
isDivide(N) :- N mod 3 =:= 0.
isDivide(N) :- N mod 5 =:= 0.
isDivide(N) :- N mod 7 =:= 0.
alternatively:
isDivide(N) :-
N mod 3 =:= 0
; N mod 5 =:= 0
; N mod 7 =:= 0.
Note that ; is not at the end of the line but at the beginning of the next line.
This is purely a matter of style but it's strongly recommended to follow it.

Resources