Blog of Andrés Aravena

Class 6: Quiz 2

02 March 2018. Deadline: Friday, 2 March, 17:00.

Read and understand all the instructions.
Ask if you do not understand.

Please download the file quiz2.R and use it as your document. Make sure that you wrote your name and student number.

1. Rabbits

Figure 1. Leonardo Fibonacci, expert on rabbits and numbers.

Near 1170, in the Italian city of Pisa, the son of Guglielmo Bonacci was born. He was named Leonardo. Nowadays, he is known as Leonardo Pisano (meaning “Leonardo of Pisa”) and also as Leonardo Fibonacci (short for “filius Bonacci”, that is, “son of Bonacci”).

Leonardo traveled with his father and, while staying in the city of Béjaïa (Algeria), he was sent to study calculation with an Arab master. He later went to Egypt, Syria, Greece, Sicily, and Provence, where he studied different numerical systems and methods of calculation. He soon realized the many advantages of the Hindu-Arabic system, as introduced first by the famous mathematician Muḥammad ibn Mūsā al-Khwārizmī (born c. 780—died c. 850 in Baghdad).

In 1202, Leonardo wrote the “Book of Calculation” which helped europeans to learn the Hindu–Arabic numerals. In that book he wrote:

A certain man put a pair of rabbits in a place surrounded on all sides by a wall. How many pairs of rabbits can be produced from that pair in a year if it is supposed that every month each pair begets a new pair which from the second month on becomes productive?

He made the following simplifying assumptions about the population:

Figure 2. Fibonacci numbers are often found in nature.

Fibonacci’s exercise was to calculate how many pairs of rabbits would remain in one year. We can see that in the second month, the first pair of rabbits reach reproductive age and mate. In the third month, another pair of rabbits is born, and we have two rabbit pairs; our first pair of rabbits mates again. In the fourth month, another pair of rabbits is born to the original pair, while the second pair reach maturity and mate (with three total pairs). After a year, the rabbit population has 144 pairs.

Beyond rabbits, it has been observed that Fibonacci numbers appear often in nature; for example, in the spirals of sunflower heads, in pine cones, in the genealogy of the male bee, in the spiral in snail shells, in the arrangement of leaf buds on a stem, and in animal horns. You can see some of them looking for “Fibonacci in nature” on Google images.

Doing some abstraction, we can use fib(n) to represent the number of pairs of rabbits on month n. The numbers have the following rules:

Your task is to write a recursive function in R that has input n and output fib(n). Remember that recursive means that the function calls itself, like in the factorial example. You can test your function by checking that at the end of the first year there are 144 pairs of rabbits.

Figure 3. You should see something like this.

2. Draw heads, hands and legs

As we discussed in class, advanced software engineering uses stick-people to model actors in “use case scenarios”. You can learn more looking for Universal Modeling Language. We want to draw one or more stick-people with the following function

Figure 4. You can also draw something like this.
draw_person <- function(size) {

Your task is to write the functions draw_head(), draw_arm() and draw_leg(). They should look like Figure 3, but you can make creative modifications, like hands with fingers, hair, etc.

The most important issue is that each of your functions must leave the turtle in the same position and the same angle as before. Your function can move the turtle as you wish, but it must leave the turtle as it was at the beginning of the function.

Hint: You are free to use any command although you only need turtle_left(), turtle_right(), turtle_forward() and turtle_backward(). Nothing else is required.

Figure 5. Examples of Trees.

3. Trees and branches

Another recursive structure found in nature are trees. Each branch is like a small tree. More precisely, a tree with n levels has branches with n-1 levels. Your task is to make a function to draw trees with three branches.

If you want to be sure of understanding the English names of the parts of a tree you can check this figure.

The function should be named tree() with three inputs: the number of levels n, the length of the trunk length, and the angle between the branches angle. Each branch is a tree with n-1 levels and with length equal to 0.8 times the length of the previous level. The first branch of every tree is angle degrees to the left of the trunk; the second is aligned with the trunk, and the last one is angle degrees to the right of the trunk.

Like in the previous exercise, the most important issue is that the tree() functions must leave the turtle in the same position and the same angle as before. Your function can move the turtle as you wish, but it must leave the turtle as it was at the beginning of the function.

You can test your function with the commands tree(6, 23, 25), tree(4, 15, 15), and tree(7, 8, 38). Each command should produce something like Figure 5.

Extra points: Modify the tree() function to make the width of each branch part equal to the level. Can you change the color of each part, like in this example?


Please send me the quiz2.R file in a single email to Do not forget to write your name and number.

Deadline: Friday, 2 March, 17:00.

Originally published at