Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If the sun shines then it is true that I get wet if it rains.
If the sun shines then it is summer.
It is not summer.
Therefore, I get wet if the sun shines.
I. A proposition symbol represents something that can be either true or false. For example claims 1, 2, and 4. Above, there are statements like “the sun shines” that can be either true or false. Define a propositional symbol for each of these statements.
II. Translate 1, 2, 3 and 4 to propositional logic sentences using your proposition symbols from (i).
Using CLP(B) in SICStus Prolog or SWI to to prove the final implication from the preceding statements:
?- sat(Sun =< (Wet =< Rain)),
sat(Sun =< Summer),
sat(~Summer),
taut(Sun =< Wet, T).
yielding:
...
T = 1,
...
This shows that the final implication follows from the previous statements.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Can Someone help me with the code for "finding if a no is armstrong no or not using prolog programming"?
I can't seem to find a solution anywhere.
Narcissistic numbers have different aliases such as Armstrong number, pluperfect digital invariant (PPDI) or plus perfect number.
numberToDigitsR(N,_,[]):-
N < 0, !.
numberToDigitsR(N,B,[N]):-
N < B, !.
numberToDigitsR(N,B,[Mod|R]):-
Mod is N mod B,
Div is N div B,
numberToDigitsR(Div,B,R).
powerList([],_,0).
powerList([H|T],E,Sum):-
powerList(T,E,TT),
Sum is (H**E)+TT.
narcissist(N,B):-
B>1,
numberToDigitsR(N,B,D),
length(D,E),
powerList(D,E,N).
tests:
?- narcissist(54748,10).
true.
?- narcissist(54748,9).
false.
?- narcissist(62,4).
true.
?- member(B,[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]), narcissist(54748,B).
B = 10 ;
false.
Thanks David for linking the wiki site.
The program first creates a list of the "digits" for a given number N and a given base B. Then it calculates the number of digits E and calculates the sum of each digit to the power of E. If the output equals the number N then this is a narcissistic number. If the base B is not known before, simply state the domain as shown in the last example.
Please note that this algorithm does not work for B=1.
Also note that the number N is given with decimal format. So if you look up numbers from this website, please convert the numbers to base 10 first.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am new to Prolog kindly assist.
Hunter, Laura, Jim, Sally, and Jack work in the same building with five adjacent offices. Hunter doesn’t work in the 5th office and Laura doesn’t work in the first office. Jim doesn’t work in the first or last office, and he is not in an office adjacent to Jack or Laura. Sally works in some office higher than Laura’s. Who works in what offices?
Write a Prolog program to solve this problem. Define what adjacency is, then what the offices are, and then create a layout(X) that allows you to put in all the rules.
Each person is put into an office that doesn’t break any of the rules given.
In this answer we use clpfd. Read a tutorial on clpfd for details!
:- use_module(library(clpfd)).
puzzle(P) :-
puzzle_vars(P, Zs),
labeling([], Zs).
puzzle_vars(P, Zs) :-
P = [hunter-Hunter, jack-Jack, jim-Jim,
laura-Laura, sally-Sally],
Zs = [Hunter,Laura,Jim,Sally,Jack],
Zs ins 1..5,
all_different(Zs),
Hunter #\= 5,
Laura #\= 1,
Jim #\= 1,
Jim #\= 5,
abs(Jim-Jack) #\= 1,
abs(Jim-Laura) #\= 1,
Sally #> Laura.
Who works in what offices? Let's ask Prolog!
?- puzzle(P).
P = [hunter-3, jack-1, jim-4, laura-2, sally-5]
; false.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have to write a rule square(S) in Prolog that tests if a number S is the square of an integer returning false (ex. square(3)) or true (ex. square(4)).
I used already a rule that generates all integers between 0 and M:
isInteger(X,M) :- between(0,M,X).
Using this generator I have to write the rule square(S). How can I do it?
Thanks
This probably will not work as a solution for your homework, but here is one of many ways to do it using constraint logic programming in ECLiPSe CLP Prolog:
:- lib(gfd).
square(S) :-
sqr(_) #= S.
It means: S is a square if it's an integer and some other value (we don't care what value, so we use "throw-out" variable _) squared equals S.
All modern Prolog systems supports constraint logic programming, and the code will be similar to above.
To solve it your way, you just need to check if S is the product of your generated integer multiplied by itself. So you could do it like this:
isInteger(X,M) :- between(0,M,X).
square(N) :-
isInteger(X, N),
N is X * X.
I came up with another possible solution, using the sqrt arithmetical function from SWI-Prolog but I think there must be a more elegant one.
I initially expected it would be as simple as X is sqrt(4), integer(X). However, this doesn't work (at least not in SWI-Prolog 7.1.4), because X is unified with the float 2.0 and integer(2.0) is false (since integer is checking the data type, not the value of the number). But this works:
square_of_integer(N) :-
Int is rationalize(sqrt(N)),
integer(Int).
It depends on first giving a representation of N as a rational (which, in SWI-Prolog, 'are represented by the compound term rdiv(N,M)'). 2 is rationalize(2.0), i.e., rationalize evaluates to an integer for round numbers.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Prolog how to add numbers in a list in a loop?
I have the loop and I would need to add the numbers into a list and render it in the end.
Prolog is a logical language and not a imperative one. You may need to formulate the problem a bit differently. By formulating what you want and not how you want it.
This is a recursive version:
the list of numbers between A and B is empty if A >= B or else
the list of numbers between A ans B is A and the list of numbers between A+1 and B
This is a version with some of prologs features.
find all numbers X between A and B
These two versions can be transferred into prolog quite directly. There is no 'loop' because prolog is not about commands (do this! do that! put that value there! increase!) but about formulating the problem.
I don't know what you mean by rendering, but you can create a list of number easily via recursion, since prolog doesn't have loops:
range_list(M,M,[M]).
range_list(M,N,[M|R]) :-
M < N ,
M1 is M+1 ,
range_list(M1,N,R)
.
range_list(M,N,[M|R]) :-
M > N ,
M1 is M-1 ,
range_list(M1,N,R)
.
You could also use built-in predicates to get what you want:
range_list(From,To,Result) :-
findall(X,between(From,To,X),Result)
.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Conditional proposition 1: If it is sunny, then I'll go.
Conditional proposition 2: I will go unless it is not sunny.
Let's decompose them as simple propositions.
A: It is sunny.
B: I will go.
Thus re-write the previous 2 conditional propositions:
1: If A, then B
2: B, unless not A
In my opinion, the truth table for each of them are:
1:
A--------B--------Proposition 1
T--------T-------------T
T--------F-------------F
F--------T-------------T
F--------F-------------T
2:
A--------B--------Proposition 2
T--------T-------------T
T--------F-------------F
F--------T-------------F <---- here is the difference.
F--------F-------------T
So I think these 2 statements are not equivalent, but the famous Discrete Mathematics and its Applications by Kenneth H. Rosen indicates that they are equivalent.
Could someone shed some light on this?
Another post is made here:
https://math.stackexchange.com/questions/129691/are-these-two-statements-equivalent
I think the issue is the word "unless." Unless is really describing when something is not true.
Conditional proposition 1: If it is sunny, then I'll go.
Conditional proposition 2: I will go unless it is not sunny. I.E. If it is not sunny, I will not go.
1: If A, then B
2: If Not B, then Not A
A ⇒ B is the same as ¬B ⇒ ¬A. I can't remember the exact name of the law, but it's easy to derive. Use the implication law to convert it to ¬A ∨ B and B ∨ ¬A and the commutative law will change B ∨ ¬A to ¬A ∨ B