Computer programs often perform computations. Here we consider some examples illustrating computations.

A program can be used to calculate future values of an investment. Consider an example where an initial sum \(s\) is invested. It could then be of interest to calculate the future value of the investment. Assuming that the value is increased by \(r\) percent each year, the value after a number of years can be calculated.

If we do, as is common in mathematics-oriented books, denote the
number of years by a variable, for example called
*n*, the value of the investment, after
*n* years, can be calculated as
$$
s_n = s \left (1 + \frac{r}{100} \right )^n
\quad \quad \quad (3)
$$
We note that the formula (3) also applies to other
situations, for example when calculating
your salary after *n* years,
given an annual raise of *r*
percent.

In a program, designed to calculate a person's salary after a given
number of years, we can use a variable
named *salary*
to represent the salary.
The raise \(r\), divided by 100, could be represented
by a variable, initalised to an annual raise of 5 percent, as

s_raise = 0.05

Using a variable
*n_years*
to represent the number of years, the salary after 5 years and after 10 years, starting
at an intial salary of 3000, can be computed by first assigning values to the variables
*salary* and
*n_years*,
and then performing calculations according to (3). Program code for performing these
operations is shown in Figure 5.

salary = 3000 n_years = 5 salary = salary * pow(1 + s_raise, n_years) print "my salary in %d years is %g" % ( n_years, salary) salary = salary * pow(1 + s_raise, n_years) print "my salary in %d years is %g" % ( 2*n_years, salary)

**Figure 5**. *Calculation of salary after 5 and 10 years, given
an intial salary and a yearly raise. *

*This the Python view - other views are
C
- Java
*

The program code in Figure 5 uses a function
called *pow*, for the purpose of
calculating the exponent in (3). The function
*pow* is part of a Python library with mathematical functions.

The program code in Figure 5 can be placed in a program and executed. The result of running the code inside a program becomes

my salary in 5 years is 3828.84 my salary in 10 years is 4886.68

As another example of computations, we consider the problem of figuring out our own weight on the moon. We may never be able to go to the moon, but nevertheless it could be interesting to see what kind of weight loss such a journey would lead to.

Here we need some physics. We might consult the great
Isaac Newton for this purpose.
It is stated by Newton, in the
law of universal
gravitation,
that given the mass \(m_{1}\)
of myself, the mass \(m_{2}\) of the planet I am standing on,
together with the radius \(R\) of the planet, my weight can
be calculated. In addition, a constant named G, having the value
\(6.674 \cdot 10^{-11}\), is needed. The resulting formula, as given by the
law of universal gravitation, reads
$$
F = G \frac{m_1 m_2}{R^2}
\quad \quad \quad (4)
$$
This formula shows how the weight, here referred to using a variable
named \(F\) to indicate that it is actually a
*force*, can be calculated.

It is now possible to calculate my weight on the moon. For this purpose I choose \(m_{1}\) in (4) to by my own mass, expressed in kilograms, and \(m_{2}\) to be the mass of the moon. In addition, I let \(R\) be the radius of the moon.

Information from Wikipedia about the moon relates the radius of the moon and the mass of the moon to the corresponding quantities for the earth, which are available from Earth Wikipedia article. The numerical values found in this way can be used, together with the formula (4) in a program as follows.

The gravity constant and the earth parameters are defined as

G = 6.674e-11 R_EARTH = 6371000.0 M_EARTH = 5.9736e24

The actual computation of the weight of a person with a mass of 77 kg,
on the moon, can then be done, using variables
*m_myself* for the mass of the person,
*m_moon* and
*r_moon* for the mass and radius of the
moon, and *weight* for the resulting
weight, as

m_myself = 77 m_moon = 0.0123 * M_EARTH r_moon = 0.273 * R_EARTH weight = G * m_myself * m_moon / \ (r_moon * r_moon) print "the weight of %g kg" % m_myself, \ " on the moon is %g N" % weight

where a printout of the result also is done. The result of executing the code gives the weight on the moon for a person with a mass of 77 kg, as

the weight of 77 kg on the moon is 124.789 N

A comparison with the weight on earth, calculated and printed using the code

weight = G * m_myself * M_EARTH / \ (R_EARTH * R_EARTH) print "the weight of %g kg" % m_myself, \ " on earth is %g N" % weight

which when executed gives the result

the weight of 1 kg on earth is 9.81986 N the weight of 77 kg on earth is 756.129 N

shows that the weight on the moon is approximately one sixth of the weight on the earth.