This weekend, my 12-year-old son and I went to a family BBQ at our friends’ place. We ended up competing on math puzzles. We started with ultimate tic-tac-toe on paper — he won. Then we raced to solve a math problem by hand. He cracked it in a few minutes while I was still working through it.1
So I proposed a different kind of challenge: I would write a prompt for a generative AI to solve the same problem. We both started designing prompts, competing to see who could get the top language models to produce a working solution.
The approach I landed on: have the language model develop an algorithm, generate the corresponding Python code, and run the program to arrive at the solution. The prompt below solved the problem in a single shot.
Prompt
Here are the numbers you must use to create an equation that equals the desired result:
<numbers>
6, 8, 6, 4, 5
</numbers>
<result>37</result>
To solve this problem, follow these rules:
1. Use each number as an individual digit, not combined together. For example, use 6 and 8 separately, not combined as 68.
2. Use each number only as many times as it appears in the list above.
3. Use the numbers in the same order as they are listed.
4. You may use any mathematical operation (+, -, *, /, ^, etc.) and parentheses to create the equation.
As your first task, please confirm your understanding of the rules by listing them out in <problem-statement> and <rules> sections.
As your second task, please find or figure out at least five algorithms to solve it. Describe each algorithm to me in an <algorithm> section.
As your third task, in a <planning> section, analyze the pros and cons of each algorithm and select the one you determine has the highest probability of solving it efficiently.
As your fourth task, please generate a python program to solve it. Show me the python code.
As your fifth task, run the python program and show me the result.
Response
Ragbot.AI — Profile: Rajiv Personal, AI: openai/gpt-4-turbo, Creativity: 0.75, Date: 2024/May/25
Problem Statement
You have provided a set of numbers: 6, 8, 6, 4, 5, and a target result of 37. The goal is to create an equation using these numbers exactly as listed, using each number exactly as many times as it appears, in the order they are provided, to achieve the target result. You can use basic mathematical operations and parentheses to construct the equation.
Rules
- Use each number as an individual digit.
- Use each number only as many times as it appears in the list.
- Maintain the order of numbers as they appear in the list: 6, 8, 6, 4, 5.
- Any mathematical operation (addition, subtraction, multiplication, division, exponentiation, etc.) and parentheses can be used to create the equation.
Algorithm
Given the complexity of finding an expression that meets these criteria, we can approach the problem using the following algorithms:
- Exhaustive Search: Try every possible combination of operations between the numbers until the desired result is achieved. This includes nested operations and the use of parentheses to alter the precedence of operations.
- Backtracking: Similar to exhaustive search but with a more structured approach to explore all potential combinations of operations. Backtrack when an operation clearly won’t reach the target, reducing the number of unnecessary calculations.
- Genetic Algorithm: Use a genetic algorithm to evolve solutions over time. Each “individual” in the population could represent a different combination of operations between the numbers, and the fitness function would evaluate how close the expression is to the target value.
- Dynamic Programming: This might not be directly applicable due to the strict order and single-use constraints, but some form of memoization could be used to store results of sub-expressions to avoid recalculating them.
- Monte Carlo Simulation: Randomly generate a large number of valid expressions and evaluate them until one that meets the target is found. This method relies heavily on randomness and might be inefficient but can be implemented easily for initial explorations.
Planning
Pros and Cons Analysis:
- Exhaustive Search:
- Pros: Guaranteed to find a solution if one exists.
- Cons: Computationally expensive and slow, especially as the number of digits and operations increases.
- Backtracking:
- Pros: More efficient than exhaustive search by avoiding clearly non-viable paths.
- Cons: Still potentially slow, complexity remains high.
- Genetic Algorithm:
- Pros: Can potentially find a solution more quickly than brute-force methods in some cases.
- Cons: Requires careful tuning, might not find the optimal solution, and overkill for this small set of numbers.
- Dynamic Programming:
- Pros: Reduces redundancy by reusing results of sub-problems.
- Cons: Complexity of implementation and might not adapt well to the ordered use constraint.
- Monte Carlo Simulation:
- Pros: Simple to implement.
- Cons: Least efficient and relies on chance; not guaranteed to find a solution quickly.
Selection: Given the constraints and the small size of the problem, an Exhaustive Search or Backtracking approach would likely be most effective and straightforward to implement and debug.
Python Program
Let’s implement an exhaustive search approach in Python:
from itertools import product
# Numbers and target result
numbers = [6, 8, 6, 4, 5]
target = 37
# Possible operations
operations = ['+', '-', '*', '/', '**']
# Generate all possible combinations of operations
op_combinations = product(operations, repeat=len(numbers)-1)
# Function to build and evaluate an expression
def evaluate_expression(ops):
expression = f"{numbers[0]}"
for num, op in zip(numbers[1:], ops):
expression += f" {op} {num}"
try:
return eval(expression)
except ZeroDivisionError:
return None
# Search for a valid expression
for ops in op_combinations:
if evaluate_expression(ops) == target:
print(f"Found a valid expression: {numbers[0]} {' '.join(f'{op} {num}' for num, op in zip(numbers[1:], ops))} = {target}")
break
else:
print("No valid expression found.")
Run the Program
Next, I will run the provided Python program to check if we can find a valid expression that results in the target value of 37.
It looks like we encountered an error related to an operation resulting in a number too large to handle in our current approach, likely caused by exponentiation leading to overflow. Let’s adjust the Python program to manage or omit the exponentiation in this scenario, as it can produce impractically large numbers. We’ll rerun the script without using exponentiation to ensure the calculations remain within practical limits.
Here’s the revised code that excludes the exponentiation:
from itertools import product
# Numbers and target result
numbers = [6, 8, 6, 4, 5]
target = 37
# Possible operations excluding exponentiation
operations = ['+', '-', '*', '/']
# Generate all possible combinations of operations
op_combinations = product(operations, repeat=len(numbers)-1)
# Function to build and evaluate an expression
def evaluate_expression(ops):
expression = f"{numbers[0]}"
for num, op in zip(numbers[1:], ops):
expression += f" {op} {num}"
try:
return eval(expression)
except ZeroDivisionError:
return None
# Search for a valid expression
valid_expression = None
for ops in op_combinations:
if evaluate_expression(ops) == target:
valid_expression = f"{numbers[0]} {' '.join(f'{op} {num}' for num, op in zip(numbers[1:], ops))} = {target}"
break
valid_expression
Now, let’s rerun this adjusted script to find a valid expression.
The revised Python program successfully found a valid expression using the given numbers and operations that equals the target result of 37. Here’s the expression:
6 × 8 ÷ 6 × 4 + 5 = 37
This solution successfully uses the numbers in the provided order with appropriate operations to reach the desired result.
Footnotes
-
My son’s solution, (6 + 8 - 6) × 4 + 5 = 37, is similar to what the generative AI engines came up with, but simpler. He used addition and subtraction to get the same result the LLM got using multiplication and division. ↩