This is a heuristic solver for crypto problems. The program solves a single problem at a time, of type problem(numbers(N1,N2,N3,N4,N5),goal(G)). Solutions are found by applying heuristics that search for certain conditions in the set of numbers, and using those conditions to generate a solution. It does this through using predicates such as oneP (determines if a number is one, or if some numbers can make one), twoP (does the same thing as oneP, but with two), pairP (determines if two numbers are a pair), onehalfP (determines if one number is half of another), onemorethangoalP, and doublegoalP. Each of these have a companion expression generation function, unless it is a preticate that only takes in one number. These functions create a value using a small order crypto solver, much the same way a person would attempt to make zero, one, or two with certain numbers if that is what they need to create the goal number from the numbers. These small order solvers function in the same way the exhaustive solver finds answers, using number pairs from the set of numbers that are passed into the problem, exhaustively analyzing all of the possible combinations of the numbers, and the values that are generated by applying basic numeric operators to them. The heuristics take the form of situation# action#. situation# consists of a series of predicates, forming the first half of a heuristic in english. action# consists of a series of expression generation functions, and an assertion of the solution that is constructed of those expressions, or just an assertion of the solution, if the heuristic is simiple enough. For example, the heuristic if the goal is zero and zero is among the numbers, then multiply all of the numbers together (a heuristic that is a part of this program), would take problem(numbers(5,0,3,6,8),goal(0)) and create the solution (5 * (0 * (3 * (6 * 8)))). The code for the situation of this heuristic would do a query for the problem with: problem(numbers(N1,N2,N3,N4,N5),goal(0)), which will only assign values to N1-N5 if the problem in the KB is one that has a goal of 0, followed by using the prolog primitive member to check if there is a 0 that is in the numbers, like so: member(0,[N1,N2,N3,N4,N5]), which means the situation only returns true if the goal is 0, and there is a 0 in the numbers of the problem. In this case, the action portion of the heuristic is a two-liner: a query for the problem - problem(numbers(N1,N2,N3,N4,N5),goal(_)) (we dont need the goal, because we know it is 0) and the assertion of the solution to the KB - assert(solution(ex(N1,*,ex(N2,*,ex(N3,*,ex(N4,*,N5)))))), forming the second half of the heuristic. Now, a more complicated heuristic wouldnt have as simple of a code, such as if the number that is one more than the goal is among the numbers and one is among the numbers and the remaing numbers can make zero, then add the zero expression to one more than the goal minus one. The situation portion of this heuristic must search through all of the numbers for one, and then through all of the remaining numbers for the number that is one greater than the goal, then evaluate if the rest of the numbers can make 0. The action portion of the heuristic must do the same, using one of the smaller order crypto solvers to solve numbers = {N1,N2,N3} goal = 0 in order to get the expression that it needs to add the solution to the KB. For more details on adding these heuristics, check out this page. You can find the code for this program here.
?- demo(50).
Problem: Numbers = {3 6 6 7 8} Goal = 5
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
application of rule 5 produces ( ( 6 - 6 ) + ( ( 7 + 8 ) / 3 ) )
Problem: Numbers = {0 8 1 8 6} Goal = 1
considering rule 1
considering rule 2
application of rule 2 produces ( 1 + ( 0 * ( 8 * ( 8 * 6 ) ) ) )
Problem: Numbers = {2 9 6 6 8} Goal = 6
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
application of rule 6 produces ( ( ( 9 - 8 ) * ( 6 + 6 ) ) / 2 )
Problem: Numbers = {8 9 9 7 7} Goal = 3
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {2 6 4 4 6} Goal = 1
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
application of rule 5 produces ( ( 6 - 6 ) + ( 2 - ( 4 / 4 ) ) )
Problem: Numbers = {6 2 9 7 7} Goal = 3
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
application of rule 5 produces ( ( 7 - 7 ) + ( ( 6 * 2 ) - 9 ) )
Problem: Numbers = {8 1 0 8 6} Goal = 4
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {0 2 6 6 5} Goal = 0
considering rule 1
application of rule 1 produces ( 0 * ( 2 * ( 6 * ( 6 * 5 ) ) ) )
Problem: Numbers = {0 0 8 0 0} Goal = 5
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {1 4 1 2 7} Goal = 4
considering rule 1
considering rule 2
considering rule 3
considering rule 4
application of rule 4 produces ( 4 + ( ( 1 - 1 ) * ( 2 * 7 ) ) )
Problem: Numbers = {1 2 4 2 9} Goal = 8
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
application of rule 6 produces ( ( ( 4 + 1 ) + ( 2 + 9 ) ) / 2 )
Problem: Numbers = {0 2 0 5 8} Goal = 5
considering rule 1
considering rule 2
application of rule 2 produces ( 5 + ( 0 * ( 2 * ( 0 * 8 ) ) ) )
Problem: Numbers = {2 8 0 0 2} Goal = 1
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
application of rule 6 produces ( ( 2 + ( 0 * ( 8 + 0 ) ) ) / 2 )
Problem: Numbers = {4 2 8 9 1} Goal = 4
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
application of rule 6 produces ( ( ( 9 + 1 ) - ( 8 / 4 ) ) / 2 )
Problem: Numbers = {6 1 4 0 5} Goal = 6
considering rule 1
considering rule 2
application of rule 2 produces ( 6 + ( 1 * ( 4 * ( 0 * 5 ) ) ) )
Problem: Numbers = {8 5 3 8 9} Goal = 0
considering rule 1
considering rule 2
considering rule 3
application of rule 3 produces ( ( 8 - 8 ) * ( 5 * ( 3 * 9 ) ) )
Problem: Numbers = {7 7 7 8 4} Goal = 0
considering rule 1
considering rule 2
considering rule 3
application of rule 3 produces ( ( 7 - 7 ) * ( 7 * ( 8 * 4 ) ) )
Problem: Numbers = {1 1 8 1 9} Goal = 3
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {5 9 8 1 3} Goal = 2
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {9 7 6 3 0} Goal = 6
considering rule 1
considering rule 2
application of rule 2 produces ( 6 + ( 9 * ( 7 * ( 3 * 0 ) ) ) )
Problem: Numbers = {5 4 4 4 6} Goal = 9
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {3 9 2 7 3} Goal = 2
considering rule 1
considering rule 2
considering rule 3
considering rule 4
application of rule 4 produces ( 2 + ( ( 3 - 3 ) * ( 9 * 7 ) ) )
Problem: Numbers = {8 6 8 6 0} Goal = 9
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {0 7 1 5 5} Goal = 3
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {8 9 8 3 5} Goal = 6
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
application of rule 5 produces ( ( 8 - 8 ) + ( ( 3 * 5 ) - 9 ) )
Problem: Numbers = {5 0 6 3 6} Goal = 0
considering rule 1
application of rule 1 produces ( 5 * ( 0 * ( 6 * ( 3 * 6 ) ) ) )
Problem: Numbers = {4 9 5 4 7} Goal = 8
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {9 7 7 4 1} Goal = 5
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
application of rule 5 produces ( ( 7 - 7 ) + ( 1 * ( 9 - 4 ) ) )
Problem: Numbers = {9 5 1 6 1} Goal = 8
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
application of rule 5 produces ( ( 1 - 1 ) + ( ( 9 + 5 ) - 6 ) )
Problem: Numbers = {0 3 5 4 7} Goal = 7
considering rule 1
considering rule 2
application of rule 2 produces ( 7 + ( 0 * ( 3 * ( 5 * 4 ) ) ) )
Problem: Numbers = {8 9 6 3 7} Goal = 3
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {2 6 8 7 4} Goal = 1
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
application of rule 6 produces ( ( ( 7 * 4 ) / ( 6 + 8 ) ) / 2 )
Problem: Numbers = {2 9 7 6 9} Goal = 6
considering rule 1
considering rule 2
considering rule 3
considering rule 4
application of rule 4 produces ( 6 + ( ( 9 - 9 ) * ( 2 * 7 ) ) )
Problem: Numbers = {0 0 5 4 7} Goal = 6
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
application of rule 5 produces ( ( 0 - 0 ) + ( 7 - ( 5 - 4 ) ) )
Problem: Numbers = {5 6 8 0 1} Goal = 0
considering rule 1
application of rule 1 produces ( 5 * ( 6 * ( 8 * ( 0 * 1 ) ) ) )
Problem: Numbers = {9 9 6 1 0} Goal = 4
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {3 4 7 9 8} Goal = 5
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {7 2 1 0 7} Goal = 5
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {8 3 1 4 1} Goal = 9
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
application of rule 5 produces ( ( 1 - 1 ) + ( 4 + ( 8 - 3 ) ) )
Problem: Numbers = {0 6 5 4 7} Goal = 6
considering rule 1
considering rule 2
application of rule 2 produces ( 6 + ( 0 * ( 5 * ( 4 * 7 ) ) ) )
Problem: Numbers = {4 7 2 4 9} Goal = 0
considering rule 1
considering rule 2
considering rule 3
application of rule 3 produces ( ( 4 - 4 ) * ( 7 * ( 2 * 9 ) ) )
Problem: Numbers = {1 9 1 1 4} Goal = 8
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
application of rule 8 produces ( ( 9 - 1 ) + ( 4 * ( 1 - 1 ) ) )
Problem: Numbers = {2 0 7 9 8} Goal = 0
considering rule 1
application of rule 1 produces ( 2 * ( 0 * ( 7 * ( 9 * 8 ) ) ) )
Problem: Numbers = {9 1 4 4 3} Goal = 2
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
application of rule 5 produces ( ( 4 - 4 ) + ( ( 9 / 3 ) - 1 ) )
Problem: Numbers = {2 1 6 3 7} Goal = 9
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
application of rule 6 produces ( ( 6 * ( 7 - ( 1 + 3 ) ) ) / 2 )
Problem: Numbers = {4 7 4 0 3} Goal = 3
considering rule 1
considering rule 2
application of rule 2 produces ( 3 + ( 4 * ( 7 * ( 4 * 0 ) ) ) )
Problem: Numbers = {0 5 0 0 7} Goal = 9
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {3 5 0 7 5} Goal = 8
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
considering rule 6
considering rule 7
considering rule 8
No Solution to this one!
Problem: Numbers = {8 7 3 3 3} Goal = 5
considering rule 1
considering rule 2
considering rule 3
considering rule 4
considering rule 5
application of rule 5 produces ( ( 3 - 3 ) + ( ( 8 + 7 ) / 3 ) )
Problem: Numbers = {5 3 8 0 9} Goal = 9
considering rule 1
considering rule 2
application of rule 2 produces ( 9 + ( 5 * ( 3 * ( 8 * 0 ) ) ) )
true .