# Combinations of numbers/operators?

**URL:** <https://www.ruby-forum.com/t/combinations-of-numbers-operators/112893>\
**Category:** Ruby\
**Created:** [August 24, 2007, 6:49am UTC](https://www.ruby-forum.com/t/combinations-of-numbers-operators/112893 "2007-08-24T06:49:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![don](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed655f/32.png) [@don](https://www.ruby-forum.com/u/don)\
**Post date:** [August 24, 2007, 6:49am UTC](https://www.ruby-forum.com/t/combinations-of-numbers-operators/112893/1 "2007-08-24T06:49:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![don](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed655f/32.png) [@don](https://www.ruby-forum.com/u/don)\
**Post date:** [August 24, 2007, 7:11am UTC](https://www.ruby-forum.com/t/combinations-of-numbers-operators/112893/2 "2007-08-24T07:11:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![don](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed655f/32.png) [@don](https://www.ruby-forum.com/u/don)\
**Post date:** [August 24, 2007, 1:20pm UTC](https://www.ruby-forum.com/t/combinations-of-numbers-operators/112893/3 "2007-08-24T13:20:08Z")

</div>

Dan Z. wrote:

> Here’s one way to do it–

Awesome! Thanks Dan.

---

<div class="post-metadata">

**Author:** ![don](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed655f/32.png) [@don](https://www.ruby-forum.com/u/don)\
**Post date:** [August 25, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/combinations-of-numbers-operators/112893/4 "2007-08-25T17:52:38Z")

</div>

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

Example:

(1/2)+(3_4) = 12.5  
1+(2_3)-4 = 3  
…

Thanks!!

---

<div class="post-metadata">

**Author:** ![don](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed655f/32.png) [@don](https://www.ruby-forum.com/u/don)\
**Post date:** [August 25, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/combinations-of-numbers-operators/112893/5 "2007-08-25T22:25:03Z")

</div>

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  
> operations” that you originally asked about.

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! 😉

---

<div class="post-metadata">

**Author:** ![don](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed655f/32.png) [@don](https://www.ruby-forum.com/u/don)\
**Post date:** [August 25, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/combinations-of-numbers-operators/112893/6 "2007-08-25T20:48:06Z")

</div>

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)+(3_4) = 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  
operations” that you originally asked about.

---

<div class="post-metadata">

**Author:** ![don](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed655f/32.png) [@don](https://www.ruby-forum.com/u/don)\
**Post date:** [August 25, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/combinations-of-numbers-operators/112893/7 "2007-08-25T22:36:16Z")

</div>

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](http://www.rubyquiz.com/quiz7.html)

Maybe the solutions will give you some ideas.

James Edward G. II
