Simple Recursion Function

Function Calculator

Let's create a logic to calculate the population after y years, given an initial population of n people and an annual growth rate of r%.

The logic can be expressed with the following formula:

(Population in year y) = (1+r/100)*(Population in year y-1)
(Population in year 0) = n

In the first line above, the population in year y represents the population we want to know, and the population in year (y-1) means the population of the previous year. Therefore, it means that the population in a given year is the previous year's population increased by r%, or r/100, and this calculation is (1+r/100) * previous year's population. The second line, population in year 0, means the initial population.

The above expression can be converted into code that a computer can understand as follows:

def population(y,n,r)=(1+r/100)*population(y-1,n,r);
population(0,n,r)= n;

You can see that only the variable y changes, while the other variables n and r are constants.

Here, two lines of code are used to define the function.

The first code tells the computer how to execute the function, and the second code tells the computer when to terminate the execution. Without the second code, an error would occur where the first code would run indefinitely.

In the first code, 'def' is a code that informs the computer that a new mathematical function is being defined. After 'def', write the function name and if there are parameters, enter them in parentheses. If there are no parameters, just write () after the function name.

Now, you need to tell the computer how to calculate the function on the right side of '='. The right-side expression '(1 + r/100) * population(y-1,n,r)' means increasing the population of year y-1 by r% by multiplying the population of year y-1 by the growth factor (1+r/100).

A function that uses itself again in its definition, as in the above definition, is called a recursive function.

The recursive definition may seem a bit confusing at first, but if you think about it carefully, you'll realize it's a very ingenious method. Let's follow the logic step by step.

If you want to know the population after 7 years when the initial population is 100 and the growth rate is 2%, you need to calculate population(7,100,2), and according to the function definition, you need to calculate (1 + 0.02) * population(6,100,2). But since we don't know population(6,100,2) yet, we need to calculate it, which according to the definition is 1.02 * population(5,100,2). But population(5,100,2) also needs to be calculated. As you can see, it's an endless process. Something is needed to end this endless process.

The second code is the code that ends this endless process. The second code 'population(0,100,2) = 100' means that when the argument is 0, instead of calculating 1.02 * population(-1,100,2) as per the definition, it simply returns 100!!

Now we have defined a recursive function called 'population'. You need to enter ';' after each code is completed to tell the computer that the code has been completed.

After defining a mathematical function on the factory page in the Function Calculator, press the run button to execute the code and then press the save button to save it to the database. Then return to the calculator page and press the F2 button to use the function you created in calculations on the calculator page.

population(7,100,2);

= 114.8685…

Comments

Popular posts from this blog

Prompt for AI

How Deep Is the Well?

Function Examples