Skip to content

Instantly share code, notes, and snippets.

@KentaKudo
Last active July 3, 2017 13:58
Show Gist options
  • Save KentaKudo/9ae24f8b9ba312e0dc1e37b49c52e0f2 to your computer and use it in GitHub Desktop.
Save KentaKudo/9ae24f8b9ba312e0dc1e37b49c52e0f2 to your computer and use it in GitHub Desktop.
Take the digits 1,2,3 up to 9 in numerical order and put either a plus sign or a minus sign or neither between the digits to make a sum that adds up to 100. For example, one way of achieving this is: 1 + 2 + 34 - 5 + 67 - 8 + 9 = 100, which uses six plusses and minuses. What is the fewest number of plusses and minuses you need to do this?
# pow(3, 8) = 6561
options = ['', '+', '-']
for i in options:
for j in options:
for k in options:
for l in options:
for m in options:
for n in options:
for o in options:
for p in options:
if eval('1' + i + '2' + j + '3' + k + '4' + l + '5' + m + '6' + n + '7' + o + '8' + p + '9') == 100:
print '1' + i + '2' + j + '3' + k + '4' + l + '5' + m + '6' + n + '7' + o + '8' + p + '9'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment