Skip to content

Instantly share code, notes, and snippets.

@leighhalliday
Created May 8, 2015 20:04
Show Gist options
  • Save leighhalliday/abbb1bec32d02269d4ee to your computer and use it in GitHub Desktop.
Save leighhalliday/abbb1bec32d02269d4ee to your computer and use it in GitHub Desktop.
# Write a program that outputs all possibilities to put + or - or nothing between the numbers 1, 2, ..., 9 (in this order) # such that the result is always 100. For example: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100.
['+', '-', ''].repeated_permutation(8).each do |perm|
sumText = ('1'..'8').zip(perm).flatten.join + '9'
sum = eval(sumText)
if sum == 100
puts "#{sumText} = 100"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment