Question

Write two clauses in PROLOG that determines if there are three values in a list that sum up to a ...

Write two clauses in PROLOG that determines if there are three values in a list that sum up to a value of N.

The output should be a single true if there exist three values whose sum is N or a single false if there are not.

If there are multiple values whose sum is N then the program should only print true once.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We just need to find that are there elements present in the list that add up to our sum

If the elements are present then we check if the size of that list is 3 if yes then our answer is true else false.

Code :

1 findthree (Sum, Answer, List) 2 Answer = L,-,-], findAny(Sun, Answer, List). % This is to ensure that our final answer only

Function Call :

findthree (12,Answer, [7,6,5,8,4,2])

Result :

Answer false [6, 4, 2]

Code :

findthree(Sum,Answer,List) :-
Answer = [_,_,_], % This is to ensure that our final answer only
findAny(Sum,Answer,List). % contains three elements

findAny(0,[],_L). % Base Case to ensure Zero sum of elements
findAny(Sum,[H|T1],[H|T2]) :-   
Currsum is Sum-H, % Take the current element in our answer and
findAny(Currsum,T1,T2).   % Make a recursive call to find furthur.

findAny(Sum,Answer,[_H|T]) :- % This will help skipping the elements
dif(Answer,[]), % so that a sublist can be formed
findAny(Sum,Answer,T).

Add a comment
Know the answer?
Add Answer to:
Write two clauses in PROLOG that determines if there are three values in a list that sum up to a ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Prolog program to find the maximum, minimum, and range of values in a list...

    Write a Prolog program to find the maximum, minimum, and range of values in a list of numbers. Please also provide the program with output.

  • In Python: Write a program that determines whether two circles intersect. The input should consist of...

    In Python: Write a program that determines whether two circles intersect. The input should consist of six numbers, x1, y1, r1 and x2, y2, r2, given as command-line arguments, and representing the center and radius of the two circles. Your program should print True if the two circles intersect, and False otherwise. As a reminder, the two circles intersect if and only if the distance of their two centers is less than or equal to the sum of their two...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • Write a program to read two integer values and print true if both the numbers end...

    Write a program to read two integer values and print true if both the numbers end with the same digit, otherwise print false. Example: If 698 and 768 are given, program should print true as they both end with 8. [Hint: The % operator can be used to find the remainder.] At the time of execution, the program should print the message on the console as: Enter two integer values : For example, if the user gives the input as...

  • MODIFY THIS LINK LIST USING C++: Write a function which determines if the values in the...

    MODIFY THIS LINK LIST USING C++: Write a function which determines if the values in the queue is divisible by 5 or 10 if true the value is divided by 5 void NumberList::listDivisibleBy5or10() ListNode* currentNode = head;   //Pointer for current value in the link list       if(!head) // checks for an empty link list    {        std::cout<<"The list is empty \n"; // tells user that the link list is empty    }    else    {   ...

  • CSE 240 Homework 5- Programming with PROLOG Due: Monday, April 22, 11:59 PM A. What This...

    CSE 240 Homework 5- Programming with PROLOG Due: Monday, April 22, 11:59 PM A. What This Assignment Is About: Facts, Rules, Goals Prolog execution model Arithmetic operations . Recursive Rules B. Use the following Guidelines Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of...

  • write a python program which finds three consecutive odd numbers whose sum adds up to 45...

    write a python program which finds three consecutive odd numbers whose sum adds up to 45 and return a list of the numbers in ascending order do not use any other packages such as numpy or math

  • Write a c++ routine that returns true if two elements in a singly linked list of...

    Write a c++ routine that returns true if two elements in a singly linked list of integers add up to a given target (similar to the two sum problem 1). Your function should return true and print the individual numbers, or return false. Examples: - If your list is: 1 → 3 → 4 → 14 → 5 and your target is 8, then you will return true and print: 3, 5 (since 3 + 5 = 8). - If...

  • Write a program that reads two integer values. It then calculates and displays the sum and...

    Write a program that reads two integer values. It then calculates and displays the sum and average of all values between them, Le. if the first value is vi and the second value is 2, then the program calculates and displays the sum and average of all values in the closed range Iv1,2 Your Program must satisfy the following constraints: A. make sure that v1 is less than 2 B. Use a function named getStats(that takes two integer yalues as...

  • Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DE...

    Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT