Pluses and Minuses 1-9

Question: Write a program that outputs all possibilities to put + or - or nothing between the numbers 1

Related Tags:

Write out the maths

This question can be solved with the divide-and-conquer paradigm. Let's break this down into mathematical equations, and try to see what we come up with.

So first, we have the base question. The function f() takes in numbers 1..9, and based on the conditions given, returns 100.

f(1..9) = 100

The question says we may use +, - or concatenate two consecutive numbers, so we can break this down into three components.

  1. 1 + f(2..9) = 100
  2. 1 - f(2..9) = 100
  3. f(12, 3..9) = 100
  1. f(2..9) = 100 - 1 = 99
  2. f(-2, 3..9) = 100 - 1 = 99
  3. f(12, 3..9) = 100
  1. 2 + f(3..9) = 99
  2. 2 - f(3..9) = 99
  3. f(23, 4..9) = 99
  4. -2 + f(3..9) = 99
  5. -2 - f(3..9) = 99
  6. f(-23, 4..9) = 99
  7. 12 + f(3..9) = 99
  8. 12 - f(3..9) = 99
  9. f(123, 4..9) = 99

Came up with a better solution or have a question? Comment below!

Next Challenge: Reversing a Linked List