A struggling manufacturer named Tennessee Instrumental is desperate to get into the calculator business. Problem is, they don't have any software engineers on payroll, and the deadline is coming up fast.

It's your job to help TI configure a basic calculus-ready calculator that can simplify expressions input as strings, and as a bonus (since they're on such a tight schedule), they don't care what language you use to get there! They do, however, care about how much space you take up. Not very good with hardware, it seems. Keep it brief, fewest bytes gets you hired.


Create a function which can simplify a basic calculus expression

Your calculator must be able to perform the following operations:

  • + Addition
  • - Subtraction and Negation
  • * Multiplication
  • / Division
  • ^ Exponentiation
  • () Parenthetical Reduction
  • ()' Derivation (first order)

Your calculator must also be able to use x as a placeholder variable. Do not solve for x. That will come in time.


Input is a string containing the plaintext version of the expression you're looking to simplify. The only valid input characters are 0-9, x, +, -, *, /, ^, (), ', and . Some examples of valid inputs are listed below:

"5 + 8"
"(5x + 3)(4x + 2)"
"4x - 9x + 15"
"24x / 6"
"5x^2 + 14x - 3"  // Use ^ for exponentiation
"(20x^2)'"        // Use ()' for derivation


Output is a string containing the plaintext version of the expression after simplification.

The only acceptable operators left over are +, -, /, and ^. Multiples of variables should be indicated as 2x, -5x, etc. Fractions should be reduced as far as possible, and parentheses and derivations must be completely eliminated, with the exception of cases such as ln(x) which syntactically cannot be made to make sense without them.

The input examples from above should output as follows:

"1"                => "1"
"5 + 8"            => "13"
"(5x + 3)(4x + 2)" => "20x^2 + 22x + 6"
"4x - 9x + 15"     => "-5x + 15"
"24x / 6"          => "4x"
"5x^2 + 14x - 3"   => "5x^2 + 14x - 3"
"(20x^2)'"         => "40x"

Last but not least, if a derivation function exists in your language, you may not use it. Derivations must be done as manually as possible. Whitespace around the basic four operators and lack of whitespace around ^ and within () is preferred.

  • \$\begingroup\$ What about if my language has a built-in symbolic calculator? Also, 1x/2 or 1/2x or 0.5x or x/2? \$\endgroup\$
    – lynn
    Commented Feb 25, 2016 at 15:30
  • \$\begingroup\$ When you say we're not allowed to use a built-in symbolic differentiator, are we allowed to use built-in functions for everything else? \$\endgroup\$
    – A Simmons
    Commented Feb 25, 2016 at 15:31
  • \$\begingroup\$ @ASimmons I'd say the rest is probably acceptable - what did you have in mind? \$\endgroup\$
    – ricdesi
    Commented Feb 25, 2016 at 15:33
  • \$\begingroup\$ @Lynn x/2 is the preferred answer, 1/2x is a different value altogether (1/(2x) reduced). \$\endgroup\$
    – ricdesi
    Commented Feb 25, 2016 at 15:35
  • 1
    \$\begingroup\$ For clarification: I voted to close as too broad, despite what the banner says. \$\endgroup\$
    – user45941
    Commented Feb 25, 2016 at 21:47

1 Answer 1


Mathematica, 159 Bytes

ToString@Expand@ToExpression[StringReplace[#,"("~~a__~~")'"->"d["~~ a ~~"]"]<>"//.{d[a_ x^n->a n x^(n-1),d[x]->1,d[a__]/;a~FreeQ~x->0,d[a__+b__]->d[a]+d[b]}"]&

NOTE: While this works on all the examples it won't currently work on all sensible differentiable input because it doesn't balance brackets when looking for derivatives. I'll change this once I've had the time to think of a sufficiently concise way of doing it. I'm sure there's some other stuff that goes wrong too, so please let me know if you spot something! There are a few bytes that are easily golfed off too, I'll get to those..


Not the answer you're looking for? Browse other questions tagged or ask your own question.