Combinations of numbers/operators?

D

Donald Patch

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.
 
D

Dan Zwell

Donald said:
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
 
D

Donald Patch

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

Dan Zwell

Donald said:
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.
 
D

Donald Patch

Dan said:
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! ;)
 
J

James Edward Gray II

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 Quiz for a problem like this:

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

Maybe the solutions will give you some ideas.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top