Some computations

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

        double 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

        double raise = 0.05; 

Using a variable

        int 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 * Math.pow(1 + raise, n_years); 
        System.out.println("my salary in " + n_years + 
            " years is " + salary); 

        salary = salary * Math.pow(1 + raise, n_years); 
        System.out.println("my salary in " + 2*n_years + 
            " years is " + salary); 

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

This the Java view - other views are C - Python

The program code in Figure 5 uses a method called pow, belonging to the class Math, for the purpose of calculating the exponent in (3). The class Math is part of the Java class library.

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.844687500001
my salary in 10 years is 4886.683880332327

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

    static final double G = 6.674e-11; 

    static final double R_EARTH = 6371000.0; 
    static final double 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); 
        System.out.print("the weight of " + m_myself + " kg"); 
        System.out.println(" on the moon is " + weight + " N"); 

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.0 kg on the moon is 124.78887740836468 N

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

        weight = G * m_myself * M_EARTH / 
            (R_EARTH * R_EARTH); 
        System.out.print("the weight of " + m_myself + " kg"); 
        System.out.println(" on earth is " + weight + " N"); 

which when executed gives the result

the weight of 1.0 kg on earth is 9.81986088519482 N
the weight of 77.0 kg on earth is 756.1292881600011 N

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