/* From Richard Wiseman It's the Friday Puzzle 2010-02-26 http://richardwiseman.wordpress.com/2010/02/26/its-the-friday-puzzle-48/ """ Imagine that you have to make the number 8 from the numbers 4, 7, 6 and 3. The rules are simple. You cannot join the numbers together (so 4 and 7 cannot become 47), have to use each number once and only once, and are only allowed to add, subtract, multiply or divide them. You might do something like this..... 4+7 = 11 11-6=5 and 5+3=8 So, here is the puzzle....can you make the number 24 with the number 5, 5, 5, and 1 (again, you cannot join the numbers together, have to use each number once and only once, and are only allowed to add, subtract, multiply or divide them)? """ The answer involves some trickery http://richardwiseman.wordpress.com/2010/03/01/answer-to-the-friday-puzzle-41/ """ Most people assume that the solution involves whole numbers. In fact, once you break that assumption, the puzzle becomes much easier. 1 / 5 = .2 5 - .2 = 4.8 4.8 x 5 =24 """ I.e.: (5 - 1/5) x 5 = 24 which is something like (a - d/b) * c where a, b, and c all can change places. Here are the output from the program: code = ((1 * (5 * 5)) - 1) = [[[5,5.5,1],24]] = [[] = 24] code = (((5 * 5) / 1) - 1) = [[[5,5.5,1],24]] = [[] = 24.0] code = ((5 * (1 * 5)) - 1) = [[[5,5.5,1],24]] = [[] = 24] code = ((5 * (5 / 1)) - 1) = [[[5,5.5,1],24]] = [[] = 24.0] code = ((5 * (5 * 1)) - 1) = [[[5,5.5,1],24]] = [[] = 24] code = ((5 / (1 / 5)) - 1) = [[[5,5.5,1],24]] = [[] = 24.0] code = (1 * ((5 * 5) - 1)) = [[[5,5.5,1],24]] = [[] = 24] code = (((5 * 5) - 1) / 1) = [[[5,5.5,1],24]] = [[] = 24.0] code = (5 * (5 - (1 / 5))) = [[[5,5.5,1],24]] = [[] = 24.0] However, the rule states that we must use all digits exactly once, which is only satisfied with this solution: code = (5 * (5 - (1 / 5))) = [[[5,5.5,1],24]] = [[] = 24.0] Note: There is no way to enforce that each of the numbers [5,5,5,1] is used exactly once. The option used in symbolic_function_induction_number_puzzle_4b.pi get_global_map().put("use_uniqe_digits",true), is not applicable here since that option requires that the digits are distinct (which is not the case in this 5,5,5,1 case), Cf http://hakank.org/jgap/number_puzzle4.conf */ data(number_puzzle_4,Data,Vars,Unknown,Ops,Constants,MaxC) :- Data = [[[5,5.5,1],24] ], Ops = new_map([infix=["+","-","*","/"]]), Constants = [1,5], Unknown = [], % No unknowns since we only use the numbers Vars = [], % No variables, just use the given numbers MaxC = 6.