1

I'm implementing a simple calculator which can only do operations sequentially, one at a time. Expressions of the form:

value1 <operator1> value2 <operator2> value3 <operator3> value4

are interpreted as

((value1 <operator1> value2) <operator2> value3) <operator3> value4

This is just like an old-school calculator, where all you have is the previous answer or value to work with.

This would work fine for something like

$$a \times b + c + d$$

Since I'm not implementing the concept of parentheses, nor operator precedence, nor memory, could an expression such as

$$(a + b) \times (c + d)$$

be expressed with this calculator?

I've tried rewriting this formula into a parenthesis-less form, but I can't seem to find a solution. However, that doesn't mean that a solution doesn't exist.

Furthermore, if the previous expression can be rewritten in the sequential form, then the next question is: can all expressions be rewritten that way?

Thanassis
  • 3,175
  • Please check the computer science related site of stackexchange for this. – neonpokharkar Sep 15 '17 at 08:01
  • 1
    This is exactly the sort of situation that Reverse Polish Notation (RPN) shines in. Can you implement that? – Deepak Sep 15 '17 at 08:24
  • Can you show us how you tried to rewrite the formula given into a form without parentheses? – Thanassis Sep 15 '17 at 09:16
  • how is it possible that a calculator have no memory? – Jaffer Wilson Sep 15 '17 at 14:02
  • 2
    I have an answer. I will try to get this question reopened so I can post it the conventional way, but there is only so much I can do. – Arthur Sep 16 '17 at 08:20
  • 1
    I do not understand why this question was closed. I think it's a perfectly valid math question, and the context seems clear to me. I edited it to make it a bit clearer. – Thanassis Sep 16 '17 at 10:14
  • are you saying that your calculator interprets $a + b \times c +d $ as $(a + b) \times (c +d) $ ?, if yes, then how will it calculate $a + (b \times c) +d $ ? – Vikram Sep 16 '17 at 10:20
  • 1
    @Vikram No, the calculator works like the 5 dollar solar powered calculators you can get everywhere: from left to right, regardless of conventional operator precedence. $a+(b\times c)+d$ can be calculated by reordering: $b\times c+a+d$. – Arthur Sep 16 '17 at 10:42
  • 1
    @Arthur, oh, thanx, btw, I voted for reopen :) – Vikram Sep 16 '17 at 10:53
  • @Deepak, postfix notation, works fine if there is a stack. The OP states that the calculator has no memory (or to be more precise, it has a single value stack, i.e. the last answer). – Thanassis Sep 16 '17 at 11:02
  • Arthur has interpreted my question the way I intended it. @Deepak, reverse polish notation won't help, as (a+b)(c+d) would be expressed as ( (+ a b)(+ c d)). Since there is no memory, this could not be calculated. To the persons who voted to close: what context or details would need to be added to reopen this question? – Frank Kusters Sep 16 '17 at 11:06
  • @Thanassis or rather it remembers the last answer along with the number you're currently typing in. – Arthur Sep 16 '17 at 11:06

1 Answers1

3

Assuming none of the numbers are zero, here is one general way to do it. First, expand out all parentheses so that what you have is a sum of products of numbers (possibly with some subtraction and division thrown in the mix). Then repeat the following:

Let $A$ be the sum of all the terms so far, and let $abc$ be the next term. Then you can add that by typing $$A\div b\div c+a\times b\times c$$Hopefully you see how to add a term which is the product of more than three numbers, and also how to deal with subtraction and division.

With this in mind, and with some small optimisation, we calculate $(a+b)(c+d)$ by typing $$ a+b\times c\div d +a+b\times d $$

Edit: More detailed general algorithm.

First expand all brackets so that the entire expression is a sum of terms on the following form: $$ \pm\frac{a_1\cdot a_2\cdots a_{i}}{a_{i+1}+\cdots+a_n} $$ A single expression of this form is trivial to calculate. If there are more than one, we can add them one by one by the following recursive process:

Say we have already added a number of these terms and gotten the result $A$, and the next term is the one I've written above. Then we can incorporate the new term into our sum by typing $$ \div a_2\div\cdots\div a_i\times a_{i+1}\times\cdots \times a_n\\\pm a_1\times\cdots \times a_i\div a_{i+1}\div \cdots \div a_n $$where the $\pm$ is the same sign as in the term above.

Small side note: This method cannot handle denominators with sums or differences in them. I don't know if that's even possible. If someone comes up with a way of calculating, say $\frac1{a+b}$ under these constrictions, please let me know.

Arthur
  • 199,419
  • Good answer (+1). It also shows the way to do this for any expression containing the operations $+ - \times \div$. You could perhaps write the general case more explicitly . – Thanassis Sep 16 '17 at 12:51
  • @Thanassis I could, and I might when I have more time. – Arthur Sep 16 '17 at 13:02
  • @Arthur Thanks for your help. Interesting answer! If it's ok with you, I can expand the answer myself in an effort to better understand the reasoning behind it. – Frank Kusters Sep 17 '17 at 12:22
  • "If someone comes up with a way of calculating, say $\frac1{a+b}$ under these constrictions, please let me know." This is solved on more advanced pocket calculators by having an $\frac1{x}$ operator. – Frank Kusters Sep 20 '17 at 09:56
  • @FrankKusters That button can work in two ways. It could invert the number in memory, or it could invert the number currently in the display (deactivating the regular digit buttons until you hit another binary operator, just like the $=$ button does). At any rate, I think $\frac{a+b}{c+d}$ might be a tough nut to crack. – Arthur Sep 20 '17 at 10:17