You start with the number 1. You can create a new number by applying an operation on two existing numbers (can be the same). The operations are +, - and *. What is the fewest number of steps needed to reach the number 2020? Bonus question: can you find multiple solutions?
Good luck!
Answer
7 is the minimum number of operations
This should be all of the shortest length solutions, some of these have already been answered, and I'll leave credit to those that found them.
I'm also including the brute force python code that I used to exhaust all the combinations. That's how I was able to arrive at the the answer to the minimum length to be what it is.
Solution 1
Found first by @hexomino
1 + 1 = 2
2 + 1 = 3
3 + 2 = 5
5 * 3 = 15
15 * 3 = 45
45 * 45 = 2025
2025 - 5 = 2020
Solution 2
1 + 1 = 2
2 + 2 = 4
4 + 1 = 5
5 + 4 = 9
9 * 5 = 45
45 * 45 = 2025
2025 - 5 = 2020
Solution 3
First found by @Jens
1 + 1 = 2
2 + 2 = 4
4 + 1 = 5
5 * 4 = 20
20 * 5 = 100
100 + 1 = 101
101 * 20 = 2020
Solution 4
Found first by @Benoit Esnard
1 + 1 = 2
2 + 2 = 4
4 + 1 = 5
5 * 4 = 20
20 * 5 = 100
100 * 20 = 2000
2000 + 20 = 2020
Solution 5
1 + 1 = 2
2 + 2 = 4
4 + 1 = 5
5 * 4 = 20
20 * 20 = 400
400 + 4 = 404
404 * 5 = 2020
Solution 6
First found by @hexomino
1 + 1 = 2
2 + 2 = 4
4 + 1 = 5
5 * 4 = 20
20 * 20 = 400
400 * 5 = 2000
2000 + 20 = 2020
Solution 7
First found by @sudhackar
1 + 1 = 2
2 * 2 = 4
4 + 1 = 5
5 + 4 = 9
9 * 5 = 45
45 * 45 = 2025
2025 - 5 = 2020
Solution 8
1 + 1 = 2
2 * 2 = 4
4 + 1 = 5
5 * 4 = 20
20 * 5 = 100
100 + 1 = 101
101 * 20 = 2020
Solution 9
First found by @Teejay
1 + 1 = 2
2 * 2 = 4
4 + 1 = 5
5 * 4 = 20
20 * 5 = 100
100 * 20 = 2000
2000 + 20 = 2020
Solution 10
1 + 1 = 2
2 * 2 = 4
4 + 1 = 5
5 * 4 = 20
20 * 20 = 400
400 + 4 = 404
404 * 5 = 2020
Solution 11
1 + 1 = 2
2 * 2 = 4
4 + 1 = 5
5 * 4 = 20
20 * 20 = 400
400 * 5 = 2000
2000 + 20 = 2020
Brute force search python code
def mdFormat(nums, ops, ans, sol_no):
#Formatting the solutions for markdown
subheader="Solution %s"%sol_no
subheader_lines='-'*len(subheader)
steps = []
val = nums[0]
ans = ans[1:]
for i, num in enumerate(nums[1:]):
steps.append('>! %s %s %s = %s
'%(val, ops[i], num, ans[i]))
val = ans[i]
s = [subheader, subheader_lines]
s.extend(steps)
s.append('\n')
return '\n'.join(s)
def apply_operations(numbers, operations):
#Gives us the new list of number choices
if len(numbers) == 1:
return [numbers[0]]
n_seq = (numbers[0], )
n = numbers[0]
for i, num in enumerate(numbers[1:]):
if operations[i] == '+':
n += num
elif operations[i] == '-':
n -= num
elif operations[i] == '*':
n *= num
n_seq += (n, )
return n_seq
solutions_found = 0
def search_n_operations(n, last_numbers=(1,), last_operations=None, choices=(1, )):
global solutions_found
if n == 0: #we're done with the recursion
return
if last_operations is None:
op_combos = (next_op for next_op in ('+', '-', '*'))
else:
op_combos = (last_operations + (next_op,) for next_op in ('+', '-', '*'))
for operation_seq in op_combos:
num_combos = (last_numbers + (next_val,) for next_val in set(choices))
for number_seq in num_combos:
new_choices = apply_operations(number_seq, operation_seq)
if new_choices[-1] == 2020: #This is an answer!
solutions_found += 1
print mdFormat(number_seq, operation_seq, new_choices, solutions_found)
if last_operations is None:
operation_seq = (operation_seq, )
search_n_operations(n - 1, number_seq, operation_seq, new_choices)
n = 10
search_n_operations(n)
print "A total of %s solutions were found for %s operations"%(solutions_found, n)
Varying the n should illustrate where the minimum bound is.
Outputs for n < 7:
A total of 0 solutions were found for 1 operations
A total of 0 solutions were found for 2 operations
A total of 0 solutions were found for 3 operations
A total of 0 solutions were found for 4 operations
A total of 0 solutions were found for 5 operations
A total of 0 solutions were found for 6 operations
No comments:
Post a Comment