# Combinations of numbers/operators?

This is a really easy one but I’m kind of a kook and new to programming
so any help is appreciated.

Let’s say I have an array consisting of 4 integers i.e. 1,2,3,4. I would
like to print every combination of these numbers using math operators
such as ±/*. The output should resemble something like:

1+2+3+4 = 10
1+2+3-4 = 2
1+2-3-4 = -4

This seems pretty straightforward and should only take a couple lines of
code (nested loops?) but I can’t quite figure out. Anyone?

Thank you.

Donald Patch wrote:

Let’s say I have an array consisting of 4 integers i.e. 1,2,3,4. I would
like to print every combination of these numbers using math operators
such as ±/*. The output should resemble something like:

1+2+3+4 = 10
1+2+3-4 = 2
1+2-3-4 = -4

Here’s one way to do it–evaluating it would be a lot more complicated
without eval(), which is our friend unless we are dealing with
user-generated input.

for op1 in [:+, :-, :, :/]
for op2 in [:+, :-, :
, :/]
for op3 in [:+, :-, :*, :/]
expression = “1 #{op1} 2 #{op2} 3 #{op3} 4”
puts “#{expression} = #{eval expression}”
end
end
end

Dan

Dan Z. wrote:

Here’s one way to do it–

Awesome! Thanks Dan.

One more thing: how would you get parentheses into the act if you also
wanted to override operator precedence?

Example:

(1/2)+(34) = 12.5
1+(2
3)-4 = 3

Thanks!!

Dan Z. wrote:

Do you know you are only dealing with four numbers? It makes quite a
difference. A general algorithm to parenthesize all possible
combinations isn’t that hard in principle (recursively split an array of
numbers, adding an operator and parentheses at each stage), but it’s
awkward to code, aspecially while maintaining the “all permutations of

Yeah, I know; this goes much farther than the original question. It
occurred to me after looking at the output that I hadn’t considered
stuff like making the first integer a negative number or the impact of
grouping/parentheses. Duh.

Here’s a related question: is there a math library in Ruby that would
allow me to “solve for” a given number given several inputs? For
example, I have the number 20 and want to know what combinations of
1,2,3,4 would yield this result? I’m not even sure what this is called
to be able to look for a solution. Thanks!

Stoopid noob!

Donald Patch wrote:

One more thing: how would you get parentheses into the act if you also
wanted to override operator precedence?

Example:

(1/2)+(34) = 12.5
1+(2
3)-4 = 3

Thanks!!

Do you know you are only dealing with four numbers? It makes quite a
difference. A general algorithm to parenthesize all possible
combinations isn’t that hard in principle (recursively split an array of
numbers, adding an operator and parentheses at each stage), but it’s
awkward to code, aspecially while maintaining the “all permutations of

On Aug 25, 2007, at 3:25 PM, Donald Patch wrote:

Here’s a related question: is there a math library in Ruby that would
allow me to “solve for” a given number given several inputs? For
example, I have the number 20 and want to know what combinations of
1,2,3,4 would yield this result?

There was an old Ruby Q. for a problem like this:

http://www.rubyquiz.com/quiz7.html

Maybe the solutions will give you some ideas.

James Edward G. II