Exhaustive Crypto Problem Solver

This is an exhaustive 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 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. This is done with just two numbers, which are passed to the function from a higher-order function, and subvalues that take place of pairs that have already been operated on. For example, problem(numbers(15,4,8,7,0),goal(8)) could find that the pair 0 * 7, which evaluates to 0, could be multiplied by (15 + 4) to get 0, which you could then add to 8 in order to get to the goal. In the demo below, youll notice that that is the solution that the program generated. When the solve() command is used for this problem, problem(numbers(15,4,8,7,0),goal(8))) is asserted to the knowledgebase using establishSpecificCryptoProblem, so that the problem can be displayed and solved, and the solution asserted to the knowledgebase. Now, this is just for specific problems passed into the solve() function. The program can also generate and solve a random problem using solve(random). When this function is run, it calls the generateRandomCryptoProblem function, which assigns 5 randomly generated numbers that fall within the range of lo-hi inclusive, where lo and hi are global variables declared (asserted to the knowledgebase) at the start of the program, and a single goal variable that falls within the same range. The numbers are passed in the same order that they were generated to the addCryptoProblemToKB function, which finds if there is already a problem in the knowledgebase, retracts it if there is one present, and then asserts the new problem to the knowledgebase. Once this is done, the program calls the displayProblem function, which searches the knowledgebase for the current problem, assigning the five numbers in the number set for the problem to five different variables, and the goal to its own variable, then uses the write() function to write out the problem in the form: NUMBERS = {N1 N2 N3 N4 N5} GOAL = G. The program then moves on to find a solution as described earlier, then adding the solution to the knowledgebase the same way a problem is added to the knowledgebase. If there is no solution to one of these problems, the program will notify the user with the phrase: No solution to this one!, otherwise, it will print out the solution by analyzing the solution that is in the knowledgebase, breaking down all of the expressions into the operators and operands, and printing those out in a fully parenthesized manner that makes the solution easy to read.

You can find the code for this program here.

Demo

?- consult(crypto.pl).
true.

?- solve(numbers(15,4,8,7,0),goal(8)).
NUMBERS = {15 4 8 7 0} GOAL = 8
Solution: ( 8 + ( ( 15 + 4 ) * ( 7 * 0 ) ) )
true .

?- solve(numbers(15,4,8,7,4),goal(15)).
NUMBERS = {15 4 8 7 4} GOAL = 15
Solution: ( ( 8 - 7 ) * ( ( 15 + 4 ) - 4 ) )
true .

?- solve(numbers(11,7,3,5,2),goal(13)).
NUMBERS = {11 7 3 5 2} GOAL = 13
Solution: ( ( ( 11 + 7 ) + ( 3 + 5 ) ) / 2 )
true .

?- solve(random).
NUMBERS = {12 14 1 10 15} GOAL = 10
Solution: ( ( 12 - 14 ) * ( ( 1 * 10 ) - 15 ) )
true .

?- solve(random).
NUMBERS = {0 3 12 13 3} GOAL = 6
Solution: ( ( 0 + 3 ) * ( 3 + ( 12 - 13 ) ) )
true .

?- demo(10).
NUMBERS = {15 7 8 13 5} GOAL = 2
Solution: ( 8 / ( ( 15 + 7 ) - ( 13 + 5 ) ) )
NUMBERS = {0 4 15 4 3} GOAL = 9
Solution: ( ( 3 * ( ( 0 + 4 ) + 4 ) ) - 15 )
NUMBERS = {6 0 9 10 14} GOAL = 2
Solution: ( 14 / ( ( 6 + 0 ) - ( 9 - 10 ) ) )
NUMBERS = {14 10 0 13 7} GOAL = 12
Solution: ( ( 14 + 0 ) - ( ( 13 + 7 ) / 10 ) )
NUMBERS = {1 4 10 8 14} GOAL = 10
Solution: ( ( 10 * 8 ) - ( 14 * ( 1 + 4 ) ) )
NUMBERS = {1 8 15 12 0} GOAL = 1
Solution: ( 1 + ( ( 8 + 15 ) * ( 12 * 0 ) ) )
NUMBERS = {13 12 6 2 6} GOAL = 2
Solution: ( 2 + ( ( 13 + 12 ) * ( 6 - 6 ) ) )
NUMBERS = {2 2 5 13 2} GOAL = 6
Solution: ( ( 2 - ( 2 + 2 ) ) - ( 5 - 13 ) )
NUMBERS = {9 11 10 13 0} GOAL = 1
Solution: ( ( 0 + ( 9 - 11 ) ) - ( 10 - 13 ) )
NUMBERS = {12 15 14 10 2} GOAL = 6
Solution: ( 2 * ( ( 12 + 15 ) - ( 14 + 10 ) ) )
true .

?- halt.