- 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
May 19, 2020
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.
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.
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.
We want to write a recursive function
cont_frac
v
as inputv
has only one element,
v
has several elements,
v
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
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?
What is the “big-picture” of the function?
Is there a special case?
Can we reduce a big problem into several small problems?
L
.L/3
L/3
L/3
.N
of length L
is made of four parts of snow level N-1
of length L/3
.To draw a snow flake we need a recursive function,
snow(N, L)
,N
and the length L
snow <- function(N, L) { # write your code here }
library(TurtleGraphics) turtle_init(mode="clip") turtle_hide() turtle_setangle(90) 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)