Created
May 8, 2015 18:52
-
-
Save bderusha/a67cdc9f7a6ea7108d11 to your computer and use it in GitHub Desktop.
Whole Number Combinations == 100
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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