Skip to content

Instantly share code, notes, and snippets.

@bderusha
Created May 8, 2015 18:52
Show Gist options
  • Save bderusha/a67cdc9f7a6ea7108d11 to your computer and use it in GitHub Desktop.
Save bderusha/a67cdc9f7a6ea7108d11 to your computer and use it in GitHub Desktop.
Whole Number Combinations == 100
#! /usr/bin/env ruby
def combinator(answer, known_val, current_val, arr)
nums = arr.dup
if nums.length.zero?
puts answer if known_val+current_val == 100
return
end
next_num = nums.shift
#add
combinator("#{answer}+#{next_num}", known_val+current_val, next_num, nums)
#sub
combinator("#{answer}-#{next_num}", known_val+current_val, -next_num, nums)
#concat
combinator("#{answer}#{next_num}", known_val, (current_val.to_s + next_num.to_s).to_i, nums)
end
combinator('1', 0, 1, [2,3,4,5,6,7,8,9])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment