We know that Computational Thinking has four parts
Today we will talk about patterns
according to the dictionary
See Also “Fur Elise for piano”
When we see a pattern we can make
There is another kind of pattern
Sometimes the easiest way to define a pattern is to use the same pattern.
For example:
To represent these patterns we use functions that are part of themselves
One useful mathematical function is factorial
The factorial of a \(n\) is \(n!\), defined as \[n! = n⋅(n-1)!\]
That is, the factorial of n
is n
times the factorial of n-1
How do we write it in R?
There is an error here. Can you find it?
Use the debugger to see what is happening
factorial(4)
visuallyfactorial(4)
visuallyfactorial(4)
visuallyfactorial(4)
visuallyfactorial(4)
visuallyEach recursive function needs an exit condition
That is, some rule to decide when to finish
There is always an easy case that does not require recursion
For example, the factorial of 1 is \(1! = 1\).
factorial
factorial(4)
find_min(c(12, 10, 15, 11, 13))
“In order to understand recursion, one must first understand recursion.”
How Recursion Works — explained with flowcharts and a video
Exercise: Homework from previous year