The Challenge

Your task is to create a program that can solve an algebraic equation.


Input will consist of a String. The string will be an equality involving the variable x, and follows the following rules:

  • the equation will always be linear
  • The valid operations in the equation are + - * /
  • Parenthesis will not be used
  • Order of operations must be respected
  • The coefficients of x will always have * in between the coefficient and x
  • You can assume that there will be exactly one solution for x


Output will be through the stdout (or an acceptable equivalent). The output should be the value of x that satisfies the equality. (You can round to the nearest thousandth if the decimal expansion is too long.)

Test cases

=> 5

=> 2.5

=> 4.667

=> 5

=> 4

=> -3


Because this is a , the shortest solution (in bytes) wins. (NOTE: You may not use built-ins that trivialize the problem.)

  • 5
    \$\begingroup\$ You should avoid edits which disqualify existing answers. \$\endgroup\$
    – user45941
    Commented Mar 8, 2016 at 2:03
  • 6
    \$\begingroup\$ In general we recommend challenges be posted to the Sandbox where they can get feedback from the community prior to being posted live. \$\endgroup\$
    – Alex A.
    Commented Mar 8, 2016 at 2:06
  • 3
    \$\begingroup\$ please don't use built-ins is neither here or there. Are they allowed or are they not? And what built-ins does this cover? \$\endgroup\$
    – Dennis
    Commented Mar 8, 2016 at 2:19
  • 3
    \$\begingroup\$ 1. please don't use built-ins is a request, not a rule. 2. What about built-ins that parse the equation but do not solve it? Can we use eval? \$\endgroup\$
    – Dennis
    Commented Mar 8, 2016 at 2:31
  • 3
    \$\begingroup\$ For the sake of clarity, we consider "request" and "rule" to be mutually exclusive. Challenge specifications should consist entirely of rules, e.g. "you must do this" or "you can't do that," rather than requests. Then it's not clear whether we actually can't do something. Do you see what I mean? \$\endgroup\$
    – Alex A.
    Commented Mar 8, 2016 at 4:45

4 Answers 4


Perl, 49 bytes

Ah, builtins got disallowed. I'll leave this solution up for reference though it is now non competing.

Includes +2 for -lp (-l can be dropped but is ugly)

Run with the equation on STDIN:

perl -lp algebra.pl <<< "x=5"


  • 1
    \$\begingroup\$ I can't tell whether this is symbolically solving the equation, but if it is then this is invalid as the OP has edited the question to disallow such built-ins. \$\endgroup\$
    – Alex A.
    Commented Mar 8, 2016 at 2:05
  • \$\begingroup\$ @AlexA. I don't think it is. \$\endgroup\$ Commented Mar 8, 2016 at 2:16
  • 2
    \$\begingroup\$ @AlexA. No, it isn't symbolically solving. Perl has no "solve". It replaces f(x)=g(x) by f(x)-g(x), then uses a builtin to evaluates this at x=0 and x=1 respectively. It then calculates where the 0 must be \$\endgroup\$
    – Ton Hospel
    Commented Mar 8, 2016 at 2:19

Javascript, 62 bytes

Without builtins, but with eval.


Uses eval to transform foo(x) = bar(x) into a function f(x) = foo(x) - bar(x), then calculates x = f(0) / (f(0) - f(1)).

  • \$\begingroup\$ Utterly explodes for input close() \$\endgroup\$ Commented Mar 8, 2016 at 4:40

Python 3, 587 bytes

from re import sub as r,findall as f
a=r(r"%s\*%s"%d,(lambda x:s(i(x.group(1))*i(x.group(2)))),a)
a=r(r"%s\/%s"%d,(lambda x:s(i(x.group(1))/i(x.group(2)))),a)
a=r(r"%s\-%s"%d,(lambda x:s(i(x.group(1))-i(x.group(2)))),a)
a=r(r"%s\+%s"%d,(lambda x:s(i(x.group(1))+i(x.group(2)))),a)
if l(a)==0 or a[0][1]!="*":a[0:0]=["1"]
if l(a)==1 or a[1][1]!="=":a[1:1]=["0"]
if l(a)==2 or a[2][1]!="*":a[2:2]=["1"]
if l(a)==3 or a[3][1]!="=":a[3:3]=["0"]
a=[int(x[0])for x in a]

The only longer answers I've seen use 80% of the code to store a big data table. But unlike eval-based solutions, it only uses e twice! (In re and len.)

Log, Day 20: Used Solve. Solve got banned. Learned re. Used re. Slept.


Matlab, 61 bytes

x=[1 0];a=eval([strrep(input(''),'=','-(') ')']);a(2)/diff(a)

Uses the same algorithm as Ton Haspel to solve the equation. Replaces the = by -( and adds a ) at the end to make the string an equation that should equal zero, and solves (Newton's method is exact for linear functions).

Input is just a string, e.g.: '3+x*2+x=x+x/2+x/2'.

Matlab has a nice builtin for this too,


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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