Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an algorithm that has the following cost:
C(Alg) <= t * Z
I've found on the web an exercise that state the following :
if t>=x, I can say that C(Alg) <= x * Z <= t * Z
but it looks strange.. do you agree?
This is in general not true. Think about the case where $Z$ is negative, then this does not hold true:
$$\text{if} x \leq Z, \text{then} xZ \leq tZ$$
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 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 4 years ago.
Improve this question
Define a predicate that converts a number of minutes into the corresponding hours and minutes by recursively subtracting 60 from the number of minutes and incrementing the number of hours. An example of using the rule is:
?- mins_to_hours_and_mins(124, H, M).
H=2 M=4
Try this one :) The main idea is to accumulate number of hours in the predicate min by subtracting 60 from total time and when the total time is lower than 60 we can unify counted hours from accumulator with H variable and rest of time with M variable.
minutes(X,H,M):- %main predicate
min(X,0,H,M).
min(X,H,ResultH,ResultM):- %ending condition of recursion
X<60,
ResultH is H, %unification with counted hours
ResultM is X.
min(X,H,ResultM,ResultH):- %recursive predicate
X >= 60,
X2 is X-60, H2 is H+1, %subtracting from total time
min(X2,H2,ResultM,ResultH).
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 5 years ago.
Improve this question
How do I get from premise ~~(AvB) to ~~(BvA).
Been working on it for a long time but I haven't found the solution yet. Appreciate the help!
Let <-> denote equivalence. We have,
~~(A v B) <-> A v B ; law of double negation
A v B <-> B v A ; because v is commutative
B v A <-> ~~(B v A) ; again, double negation
Thus
~~(A v B) <-> ~~(B v A)
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.
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)
.