May 19, 2020

Our course so far

  • Solve simple problems
    • Decomposition
    • Pattern matching
    • Abstraction
  • Systems Analysis
    • Identify the system parts and processes
    • Use R to simulate a system
    • See what happens in the long term
    • See the effect of changing rates or initial conditions

Computational Thinking

  • People have been using numbers since prehistoric times.

  • Humans wrote numbers before writing words.

  • Over 2600 years ago in Babylon, people used fraction to represent rational numbers.

  • The problem is that some numbers have a lot of decimals and it is hard to find the fractions.

Continuous Fractions

In India, 1500 years ago, the mathematician Aryabhata found that numbers with a lot of decimals can be represented by an expression like this: \[1.43333333333333\ldots =1+\frac{1}{2+\frac{1}{3+\frac{1}{4}}}\]

If the number has more decimals, then the fraction continues.

This is called a continuous fraction.

Continuous Fractions

In general, continuous fractions are characterized by a list of numbers \([a_1, a_2, a_3, a_4, …]\) that represents the number \[a_1+\frac{1}{a_2+\frac{1}{a_3+\frac{1}{a_4+\cdots}}}.\]

This means that every numeric vector can be evaluated as a continuous fraction.

Calculating the decimal value

We want to write a recursive function

  • called cont_frac
  • takes a vector v as input
  • returns the value of the continuous fraction


  • If the vector v has only one element,
    • then the result is that element
  • If the vector v has several elements,
    • then the result is the first element of v
    • plus 1 divided by the continuous fraction of the remaining elements of v


If all is right, you should get the following results:

cont_frac(c(1, 2, 2, 2, 2, 2, 2, 2, 2, 2))
[1] 1.414214
cont_frac(c(3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3))
[1] 3.141593

How to solve it

You have to understand the problem

  • What is the input?
  • What should be the output?
  • What are the rules?

Find the connection between input and output

Have you seen it before?

Or have you seen the same problem in a slightly different form?

What kind of problem is this?

Can you solve manually some simple cases?

Test some code

What is the “big-picture” of the function?

Is there a special case?

Can we reduce a big problem into several small problems?

Turtle in the Snow

A snowflake is a shape like the figure below.

As you can see there are different levels of snow.

  • Snow level 1 is just a straight line of length L.
  • Snow level 2 is made of four parts,
  • each part is a straight line of length L/3
  • that is, each part is snow level 1 of length L/3
  • Snow level 3 has also four parts,
    • each made of snow level 2 of length L/3.
  • In general snow level N of length L is made of four parts of snow level N-1 of length L/3.

Turtle movements

  • draws the first part,
  • turns left 60 degrees,
  • draws the second part,
  • turns 120 degrees to the right,
  • draws another part,
  • turns 60 degrees left,
  • and draws the last part.

Recursive solution

To draw a snow flake we need a recursive function,

  • called snow(N, L),
  • with two inputs: the level N and the length L
snow <- function(N, L) {
    # write your code here

Test it with the following code

turtle_setpos(10, 1)
snow(1, 80)
turtle_setpos(10, 5)
snow(2, 80)
turtle_setpos(10, 20)
snow(3, 80)
turtle_setpos(10, 37)
snow(4, 80)
turtle_setpos(10, 55)
snow(5, 80)
turtle_setpos(10, 75)
snow(6, 80)