Untitled
unknown
plain_text
5 months ago
6.6 kB
1
Indexable
1) Write a program in prolog for map coloring problem. Code:- % Define adjacent regions adjacent(a, b). adjacent(b, c). adjacent(c, a). % Define possible colors color(red). color(blue). color(green). % Assign colors to regions map_coloring(a-Region1Color, b-Region2Color, c-Region3Color) :- color(Region1Color), color(Region2Color), color(Region3Color), adjacent(a, b), adjacent(b, c), adjacent(c, a), Region1Color \= Region2Color, Region2Color \= Region3Color, Region3Color \= Region1Color. % Query to color the map color_map(Colors) :- map_coloring(a, b, c), Colors = [a-Region1Color, b-Region2Color, c-Region3Color]. Output:- Query:- To find a valid cloring for the regions a, b and c use:- ?- color_map(Colors). O/P:- Colors=[a-red, b-blue, c-red]. This indicates that regions a is colored red, region b is blue and region c is red, satisfying the condition that no two adjacent regions have the same color. 2) Write a program in prolog for tower of hanoi problem. Code:- % Move N disks from Source to Destination using Auxiliary rod hanoi(0, _, _, _) :- !. hanoi(N, Source, Destination, Auxiliary) :- N > 0, M is N - 1, hanoi(M, Source, Auxiliary, Destination), write('Move disk '), write(N), write(' from '), write(Source), write(' to '), write(Destination), nl, hanoi(M, Auxiliary, Destination, Source). % Example query solve_hanoi(N) :- hanoi(N, source, destination, auxiliary). Output:- ?- solve_hanoi(3). Move disk 1 from source to destination Move disk 2 from source to auxiliary Move disk 1 from destination to auxiliary Move disk 3 from source to destination Move disk 1 from auxiliary to source Move disk 2 from auxiliary to destination Move disk 1 from source to destination 3) Write a program in prolog for designing basic calculator for performing addition, substraction, multiplication and division. Code:- % Addition calculate(add, X, Y, Result) :- Result is X + Y. % Subtraction calculate(subtract, X, Y, Result) :- Result is X - Y. % Multiplication calculate(multiply, X, Y, Result) :- Result is X * Y. % Division calculate(divide, X, Y, Result) :- Y \= 0, % Ensure divisor is not zero Result is X / Y. % Handle division by zero calculate(divide, _, 0, 'Error: Division by zero') :- !. Output:- ?- calculate(add, 5, 3, Result). Result = 8. ?- calculate(subtract, 5, 3, Result). Result = 2. ?- calculate(multiply, 5, 3, Result). Result = 15. ?- calculate(divide, 6, 3, Result). Result = 2.0. ?- calculate(divide, 6, 0, Result). Result = 'Error: Division by zero'. 4) Write a program in prolog for odd and even numbers. Code:- % Check if a number is even is_even(Number) :- Number mod 2 =:= 0. % Check if a number is odd is_odd(Number) :- Number mod 2 =:= 1. Output:- ?- is_even(4). true. ?- is_even(5). false. ?- is_odd(5). true. ?- is_odd(4). false. 5) Write a program in prolog to check whether the entered number is prime number or not. Code:- % Check if a number is prime is_prime(2) :- !. is_prime(N) :- N > 2, N mod 2 =\= 0, \+ has_factor(N, 3). % Check if N has a factor other than 1 and itself has_factor(N, F) :- N mod F =:= 0. has_factor(N, F) :- F * F < N, NextF is F + 2, has_factor(N, NextF). Output:- ?- is_prime(7). true ?- is_prime(9). false. ?- is_prime(4). false. Q6) Write a program to determine if two people are siblings. Code:- % Define sibling relationships sibling(john, mary). sibling(mary, john). sibling(susan, alice). sibling(alice, susan). % Check if two people are siblings are_siblings(X, Y) :- sibling(X, Y). Output:- ?- are_siblings(john, mary). true. ?- are_siblings(john, susan). false. Q7) Write a program in prolog to compute the length of a list. Code:- % Compute the length of a list list_length([], 0). list_length([_|Tail], Length) :- list_length(Tail, TailLength), Length is TailLength + 1. Output:- ?- list_length([1, 2, 3, 4, 5], Length). Length = 5. ?- list_length([a, b, c], Length). Length = 3. Q8) Write a program in prolog to find minimum element in a list. Code:- % Rule min_list([X], X). min_list([H|T],Min):-min_list(T, MinT), Min is min(H, MinT). Output:- min_list([5,3,4,6,7], Min). Min=3 Q9) Write a program in prolog to find square of a number. Code:- % Rule to calculate the square of a number square(Number, Square):- Square is Number * Number. Output:- ?- square(5, Result). Result=25. Q10) Write a program in prolog to find cube of a number. Code:- % Rule to calculate the cube of a number cube(Number, Cube):- Cube is Number * Number * Number. Output:- ?- cube(3, Result). Result=27. Q11) Write a program in prolog to find maximum from three numbers. Code:- % Rule to find maximum from three numbers max_of_three(A, B, C, Max):- Max is max(A, max(B, C)). OUTPUT:- ?- max_of_three(4, 9, 7, Max). Max=9. Q12) Write a program in prolog to find minimum from three numbers. Code:- % Rule to find minimum from three numbers min_of_three(A, B, C, Min):- Min is min(A, min(B, C)). OUTPUT:- ?- min_of_three(4, 9, 7, Min). Min=4. Q13) Write a program in prolog to concatenate two strings together. Note:- in order to concatenate two strings in prolog we use built in predicate atom_concat/3. Code:- % Rule to concatenate two atoms(strings) concat_atoms(Atom1, Atom2, Result):- atom_concat(Atom1, Atom2, Result). Output:- ?- concat_atoms('Hello', 'World', Result). Result= 'HelloWorld'. Q14) Write a program in prolog for simple facts about pets. Code:- % Facts about pets dog(bella). dog(max). cat(whiskers). cat(socks). % Rule to determine if an animal is a dog is_dog(Animal):- dog(Animal). % Rule to determine if an animal is a cat is_cat(Animal):- cat(Animal). Output:- Query:- 1) Is whiskers a cat? ?- is_cat(whiskers). true. 2) List all dogs. ?- is_dog(Dog). Dog=bella. Dog= max. Q15) Write a program in prolog for simple person age facts. Code:- % Facts about ages age(john, 25). age(mary, 30). age(alice, 22). % Rule to find age of a person person_age(Person, Age):- age(Person, Age). Output:- query1:- What is the alice age? ?- person_age(alice, Age). Age=22. query2:-List all people and their ages. ?- person_age(Person, Age). Person=john, Age=25; Person=mary, Age=30; Person=alice, Age=22.
Editor is loading...
Leave a Comment