clpfd

Finding all cube roots with a number as a limit

末鹿安然 提交于 2019-12-12 19:12:24
问题 I want to find all of the cube roots that their cubes + their remainder add up to a number to user inputs. So for example, the query: ?- smallerCube(X,20). Would give the result: 1 remainder 19 2 remainder 12 Since 1^3 = 1 (remainder 19), 2^3 = 8(remainder 12) and 3^3 = 27 which is bigger than the initial input of 20, and hence it's not being calculated here. So far this is my code: cubeLess(X,B,R) :- X =< B, X1 is X*X*X, R is B-X1. smallerCube(X,B) :- int(X), X2 is X*X*X, X2 =< B, cubeLess

how to do Arithmetic Operations in DCG in prolog

为君一笑 提交于 2019-12-12 18:18:58
问题 verb_phrase(X,P)--> trans_verb(X,X+1,P1), noun_phrase(X+1,P1,P). For the code above, if X=1, I will get (...1+1...). "..."means not important code. but I really want to get 2 instead of 1+1. Could someone tell me how to do it? 回答1: If you are reasoning over integers, the cleanest way is to use CLP(FD) constraints for arithmetic. You can use {}/1 within DCGs to embed Prolog goals. For example: :- use_module(library(clpfd)). verb_phrase(X0, P)--> { X #= X0 + 1 }, trans_verb(X0, X, P1), noun

How to Solve Cryptarithmetic Puzzle in Prolog

不羁的心 提交于 2019-12-12 10:56:48
问题 I have to write a Prolog program for solving a cryptarithmetic puzzle. I need to write a function solve([A, M, P, D, Y]) which assigns the variables [A, M, P, D, Y] to values from 0 to 9 so that it satisfies the equation AM+PM=DAY. Each variable is assigned to a different value, and A, P, and D cannot be equal to 0. I started writing this function, but ran into problems running my program. I set the restrictions of A, P, and D not being zero. As I was going through the algorithm, I realized

Counting occurrences in list

雨燕双飞 提交于 2019-12-11 10:55:35
问题 I'm trying to create a rule that counts the number of occurrences of a certain element in a given list, what I've tried so far does not seem to work the way I expect: The first argument here should be the list, the second one the element we are looking for, and the last one the number of occurrences: %empty list should always return 0 occurences count([],E,0) :- true. %if our head is what we are looking for, count count([E|T],E,N) :- count(T,E,N-1). %otherwise, do not count count([H|T],E,N) :

End View Puzzle, Prolog

孤人 提交于 2019-12-11 09:58:50
问题 I'm trying to write a solver for End View Puzzles, using CLPFD (for those who are unfamiliar, here's a simple description http://www.funwithpuzzles.com/2009/12/abcd-end-view-a1.html ). I'm working on the constraint that I'll apply to each row/column, and running into some trouble. So I'm thinking it should look something like this: % NxN board, numbers from 0 to M in the row, Left/Right are the clues % corresponding to the row endviews(N,M,List,Left,Right):- length(List,M), domain(List,0,M),

Trying to count steps through recursion?

a 夏天 提交于 2019-12-11 08:43:36
问题 This is a cube, the edges of which are directional; It can only go left to right, back to front and top to bottom. edge(a,b). edge(a,c). edge(a,e). edge(b,d). edge(b,f). edge(c,d). edge(c,g). edge(d,h). edge(e,f). edge(e,g). edge(f,h). edge(g,h). With the method below we can check if we can go from A-H for example: cango(A,H). move(X,Y):- edge(X,Y). move(X,Y):- edge(X,Z), move(Z,Y). With move2, I'm trying to impalement counting of steps required. move2(X,Y,N):- N is N+1, edge(X,Y). move2(X,Y

Can you use clpfd to implement a coverage algorithm?

自作多情 提交于 2019-12-11 03:54:24
问题 Say I want to find the set of features/attributes that differentiate two classes in a simple matching manner can I use clpfd in prolog to do this? c_s_mining(Features,Value):- Features = [F1,F2,F3,F4], Features ins 0..1, ExampleA = [A1,A2,A3,A4], ExampleB =[B1,B2,B3,B4], ExampleC =[C1,C2,C3,C4], A1 #=0, A2#=1,A3#=0,A4#=1, B1 #=0, B2#=1,B3#=0,B4#=1, C1 #=1, C2#=0,C3#=0,C4#=1, ExampleD =[D1,D2,D3,D4], ExampleE =[E1,E2,E3,E4], ExampleQ =[Q1,Q2,Q3,Q4], D1#=1,D2#=0,D3#=1,D4#=0, E1#=1,E2#=0,E3#=1

Prolog: partition integer list items by their parity

女生的网名这么多〃 提交于 2019-12-11 03:29:10
问题 Write a predicate which takes as input a list of integers, L , and produces two lists: the list containing the even elements from L and the list of odd elements from L . ?- separate_parity([1,2,3,4,5,6], Es, Os). Es = [2,4,6], Os = [1,3,5] ? ; no 回答1: Just use structural recursion on lists. Write down the equivalences for each mutually exclusive case: parity_partition([A|B], [A|X], Y):- 0 is A mod 2, parity_partition(B,X,Y). parity_partition([A|B], X, [A|Y]):- 1 is A mod 2, parity_partition(B

How to prevent Prolog from backtracking where it shouldn't

送分小仙女□ 提交于 2019-12-11 02:46:43
问题 I'm trying to solve a CSP where I need to distribute cocktails over bartenders so that each bartender has at most one cocktail and all cocktails are given a bartender. I solved it by creating a list of clpfd variables,first giving them the full domain of all bartenders and then removing all bartenders that don't know how to make that cocktail. My code works, but there is one problem: it's too slow. If I look in the profiler, remove_domain gets called 2000 times(for the input I'm giving my

Puzzle taken from Gardner

∥☆過路亽.° 提交于 2019-12-11 01:52:17
问题 I'm trying to solve the following puzzle in Prolog: Ten cells numbered 0,...,9 inscribe a 10-digit number such that each cell, say i, indicates the total number of occurrences of the digit i in this number. Find this number. The answer is 6210001000. This is what I wrote in Prolog but I'm stuck, I think there is something wrong with my ten_digit predicate: %count: used to count number of occurrence of an element in a list count(_,[],0). count(X,[X|T],N) :- count(X,T,N2), N is 1 + N2. count(X,