Software & OS

How to Solve Math Problems on a Computer: Calculator to SymPy

To solve math problems on a computer, match the tool to the problem: a calculator app for arithmetic, Python's built-in fractions module for exact fractions, and the SymPy library for algebra with unknowns. Install SymPy with pip install sympy, then solveset(Eq(x**2, 1), x) returns {-1, 1}.

This guide covers those three levels, in the order you are likely to need them. Each example is short enough to type in yourself.

Which tool for which kind of problem

ProblemToolResult
Arithmetic, percentages, order of operationsCalculator app (Scientific mode)A decimal number
Fractions that must stay exactPython fractions moduleAn exact fraction
Solve for x, systems, differential equationsSymPyExact symbolic answers
Date differences, unit conversion, number basesCalculator app modesA number or date

The rule of thumb: if you are looking for a number, a calculator is enough. If you are looking for a formula or an exact value, you want a symbolic tool.

Solve arithmetic with the calculator app

Every desktop operating system ships a calculator. On Windows, the open-source Calculator app has several modes. Its README describes Standard mode as evaluating "commands immediately as they are entered", while Scientific mode "evaluates commands using order of operations". That difference matters for expressions like 2 + 3 * 4: the correct answer by the order of operations is 14, so use Scientific mode and check that result when a problem mixes addition and multiplication.

The same app has a Date Calculation mode for the difference between two dates, a Programmer mode for conversion between number bases, and a unit converter. If your problem is "how many days between two dates" or "what is this number in hexadecimal", use those modes before reaching for code.

A calculator has limits. It returns a rounded decimal, it cannot take an unknown, and it will not show you steps. When a problem contains a letter, move to the next section.

Use Python as a calculator

If Python is installed (see how to install Python on Windows), start the interpreter by typing python and enter expressions directly. Python follows the usual order of operations, and ** is the power operator.

>>> 2 + 3 * 4
14
>>> (2 + 3) * 4
20
>>> 2 ** 10
1024
>>> 17 % 5
2

Example output. % is the remainder, and // is division that rounds down.

Keep fractions exact with the fractions module

Decimal results hide a trap. Computers store ordinary floats in binary, so many short decimals are stored as close approximations. The Python documentation says so directly: Fraction(1.1) "is not exactly equal to 11/10". The fix is the fractions module, which is part of the standard library, so there is nothing to install.

>>> from fractions import Fraction
>>> Fraction(16, -10)
Fraction(-8, 5)
>>> Fraction('3/7')
Fraction(3, 7)
>>> Fraction(1, 3) + Fraction(1, 6)
Fraction(1, 2)

Example output. A Fraction is always reduced to lowest terms with the sign on the numerator. Build fractions from integers or strings, not from floats. If you are stuck with a float, Fraction(1.1).limit_denominator() returns Fraction(11, 10), which is the closest fraction with a denominator of at most 1,000,000.

A zero denominator raises ZeroDivisionError, the same as dividing by zero anywhere else in Python.

Solve for x with SymPy

SymPy is a Python library for symbolic mathematics. It treats x as an unknown rather than a number, so it can solve equations, not just evaluate them. Install it with the command from its installation page:

pip install sympy

If you work on several projects, install it inside a virtual environment. How to create a Python virtual environment with venv shows the steps. Then set up the symbols you want to use:

>>> from sympy import *
>>> x, y, z = symbols('x y z')

The wildcard import is what the SymPy tutorial uses, and it is fine in an interactive session. In a saved script, import only the names you need, for example from sympy import symbols, Eq, solveset.

One equation with one unknown

solveset is the function the SymPy tutorial recommends. Pass it an equation and the variable. An expression not wrapped in Eq is treated as being equal to 0.

>>> solveset(Eq(x**2, 1), x)
{-1, 1}
>>> solveset(x**2 - 1, x)
{-1, 1}
>>> solveset(x**2 - x, x)
{0, 1}

These three lines are taken from the SymPy tutorial. The first two are the same problem written two ways. Both give the exact answers, with no rounding.

No solution, every solution, and "cannot find it"

The result is a set, so it can say more than a calculator can:

>>> solveset(exp(x), x)
∅
>>> solveset(x - x, x, domain=S.Reals)
ℝ

The empty set ∅ means no solution exists: exp(x) is never 0. ℝ means every real number works, because x - x is 0 for any x. The third case is the one to watch for. For cos(x) - x = 0, SymPy cannot produce a closed-form answer, and it returns the condition back to you instead of a number:

>>> solveset(cos(x) - x, x)
{x │ x ∊ ℂ ∧ (-x + cos(x) = 0)}

That output means "SymPy did not solve this", not "there is no answer". For equations like this you need a numerical method, which gives an approximate value rather than a formula.

The older solve function

solve is the older interface and returns a list. The tutorial notes it handles some cases that solveset does not, such as equations whose answer involves the Lambert W function:

>>> solve(x*exp(x) - 1, x)
[W(1)]

Try solveset first. If it returns an unevaluated set, try solve on the same equation.

Differential equations

For equations that contain a derivative, declare a function symbol and use dsolve. This example is from the SymPy tutorial:

>>> f, g = symbols('f g', cls=Function)
>>> diffeq = Eq(f(x).diff(x, x) - 2*f(x).diff(x) + f(x), sin(x))
>>> dsolve(diffeq, f(x))
f(x) = (C₁ + C₂⋅x)⋅ℯ^x + cos(x)/2

The constants C1 and C2 stand for the arbitrary constants of integration. They print as C₁ and C₂ when unicode printing is on. The formatting above is simplified to one line; SymPy's own output is laid out as a two-dimensional expression.

Check the answer before you trust it

A computer algebra system does not understand your problem, only the equation you typed. Two habits catch most mistakes:

  • Substitute the answer back in. For x**2 - x with answers 0 and 1, check that 0**2 - 0 and 1**2 - 1 are both 0. This is arithmetic you can do by hand.
  • Check the domain. solveset accepts a domain argument, as in domain=S.Reals above. A word problem about lengths or prices usually needs real, and often positive, answers, so discard solutions that make no sense in the situation.

Also check how you typed the equation. In Python, ^ is not a power operator, and x2 is a new variable name, not x squared. Write x**2.

If it still doesn't work

If pip install sympy says pip is not recognized, Python is not on your PATH or is not installed. If solveset raises NameError: name 'solveset' is not defined, you skipped the from sympy import ... line in this session. If you get NameError: name 'x' is not defined, you have not created the symbol: run x = symbols('x') first, because Python does not allow using a name that has never been assigned.

Frequently asked questions

What is the easiest way to solve an equation on a computer?
For a single equation with one unknown, install SymPy with pip install sympy and call solveset on it. It returns exact answers such as {-1, 1} instead of rounded decimals. For plain arithmetic, the calculator app that comes with your operating system is enough.
Can Python solve math problems without installing anything?
Yes, for arithmetic. Python evaluates expressions such as 3 * (4 + 5) directly, and the standard library fractions module does exact fraction arithmetic. Solving for an unknown x needs a third-party package such as SymPy.
Why does a computer give 0.30000000000000004 for 0.1 + 0.2?
Python floats are stored in binary, and 0.1 and 0.2 cannot be written exactly in binary, so the sum is very slightly off. Use the fractions module or the decimal module when you need exact results.
Does solveset find every solution?
No. The SymPy documentation shows that for an equation like cos(x) - x = 0 it cannot find a closed form and returns an unevaluated set. The older solve function handles some cases solveset does not, such as equations that need the Lambert W function.

Sources