Page 1 of 1

Auto Solve Parentheses in math equality??

Posted: Tue Sep 13, 2022 7:08 pm
by zikitrake
Do you know a tool that automatically adds parentheses to a mathematical equality?

For example, given an input:
2 + 6 / 2 + 3 * 2 + 4 = 22

That automatically solves:
(2 + 6) / 2 + 3 * (2 + 4) = 22

Thank you!

Re: Auto Solve Parentheses in math equality??

Posted: Tue Sep 13, 2022 9:32 pm
by skywalk
Assuming : operator = division?
You could parse the equation and solve all combinations of parentheses until solution agrees with equality.

Re: Auto Solve Parentheses in math equality??

Posted: Wed Sep 14, 2022 7:48 am
by zikitrake
skywalk wrote: Tue Sep 13, 2022 9:32 pm Assuming : operator = division?
You could parse the equation and solve all combinations of parentheses until solution agrees with equality.
Yes, ":" = "/"... I just changed it :)

I don't know how to get all the combinations and do the calculation on them :oops:

2 + 6 / 2 + 3 * 2 + 4
(2 + 6) / 2 + 3 * 2 + 4
2 + 6 / (2 + 3 * 2) + 4
(2 + 6 / 2) + 3 * (2 + 4)....

Re: Auto Solve Parentheses in math equality??

Posted: Wed Sep 14, 2022 8:14 am
by STARGÅTE
What you have to think of is, these calculations are like trees.
You have leaves like all the numbers and you have nodes like all the operators.

Code: Select all

Nodes:     +   :   +   *   +

Leaves:  2   6   2   3   2   4
Now, you can add edges to connect the leaves with the nodes, and the nodes to each other, like:

Code: Select all

                   + ---
                 /       \
               :          +
             / |        / |
Nodes:     +    \      *    \ 
          / \    |    / \    |
Leaves:  2   6   2   3   2   4
At this point, you can change the edges randomly or in a deterministic way, and check all the results.
When you find a solution, you can create the inline-form of the tree and you can set the needed parentheses depending on the operator precedence:

Re: Auto Solve Parentheses in math equality??

Posted: Wed Sep 14, 2022 9:16 am
by idle
Search for rpn eval Reverse polish notation, shunting yard algorithm

Re: Auto Solve Parentheses in math equality??

Posted: Wed Sep 14, 2022 10:30 am
by zikitrake
Really thank you, @idle and @STARGÅTE for yours tips, but it's too advanced for me :oops: :oops:

My son has a hard time solving this type of exercise (putting parentheses where they belong) and I was wondering if there was an online utility that would do it automatically so he can practice/check it out.

Again, thanks anyway!

Re: Auto Solve Parentheses in math equality??

Posted: Fri Sep 16, 2022 8:41 pm
by Nituvious
In my opinion math problems like this are ambiguous on purpose when parentheses are omitted from them. I think it comes down to personal preference in this case. Buuut, I'm not a math wiz so I'm sure there's more to it than trying to apply PEMDAS(or any of those alternative acronyms!) to all of life's problems.

How should these types of problems be solved when order of operation can go several directions?

edit: oh, wait. My bad, I completely glossed over the part where it's already solved haha :mrgreen: