/* 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. Using unique_vars_all=true [program = A * (C - D / B),res = 24.0,count = 52] [program = (C - D / B) * A,res = 24.0,count = 28] [program = (A - D / B) * C,res = 24.0,count = 22] [program = C * (B - D / A),res = 24.0,count = 8] [program = C * (A - D / B),res = 24.0,count = 6] [program = (C - D / A) * B,res = 24.0,count = 1] resultMap = [24.0 = 6] Cf http://hakank.org/jgap/number_puzzle4.conf */ /* In this version, we must weed out the valid solutions manually, i.e. those with exactly three 5s and one 1s. */ /* data(number_puzzle_4,Data,Vars,Unknown,Ops,Constants,MaxSize,Params) :- Data = [[[5,5,5,1],24] ], Ops = [+,-,*,/], Constants = [1,5], Unknown = [], % No unknowns since we only use the numbers Vars = [], % No variables, just use the given numbers MaxSize = 6, Params = new_map([num_gens=30]). */ % Using unique_vars_all=true much is neater. data(number_puzzle_4,Data,Vars,Unknown,Ops,Constants,MaxSize,Params) :- Data = [[[5,5,5,1],24] ], Ops = [+,-,*,/], Constants = [], % No constants is used. Unknown = [5,5,5,1], % No unknowns since we only use the numbers Vars = ['A','B','C','D'], % No variables, just use the given numbers MaxSize = 6, Params = new_map([init_size=200, num_gens=1800, unique_vars_all=true ]).