Auto Solve Parentheses in math equality??

For everything that's not in any way related to PureBasic. General chat etc...
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Auto Solve Parentheses in math equality??

Post 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!
Last edited by zikitrake on Wed Sep 14, 2022 7:48 am, edited 1 time in total.
PB 6.21 beta, PureVision User
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Auto Solve Parentheses in math equality??

Post by skywalk »

Assuming : operator = division?
You could parse the equation and solve all combinations of parentheses until solution agrees with equality.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Re: Auto Solve Parentheses in math equality??

Post 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)....
PB 6.21 beta, PureVision User
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Auto Solve Parentheses in math equality??

Post 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:
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Auto Solve Parentheses in math equality??

Post by idle »

Search for rpn eval Reverse polish notation, shunting yard algorithm
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Re: Auto Solve Parentheses in math equality??

Post 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!
PB 6.21 beta, PureVision User
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Auto Solve Parentheses in math equality??

Post 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:
▓▓▓▓▓▒▒▒▒▒░░░░░
Post Reply