In Class: Week 11, recursion
Create a week11 subdirectory
in your cs21/class directory and copy over my starting point files:
$ cd
$ cd cs21/class
$ pwd
/home/your_user_name/cs21/class
$ mkdir week11
$ cd week11
$ pwd
/home/your_user_name/cs21/class/week11
$ cp ~newhall/public/cs21/week11/* .
$ ls
recursion.py
- Open recursion.py. We are going to write some iterative and
recursive versions of the same function. Iterative versions use
loops, recursive versions do not use loops, and instead contain
calls to themselves. The idea of recursive functions is based on
recursive definitions. For example, n! can be defined
as n(n-1)!.
- Lets try writing recursive factorial together
- Next, try writing an iterative version (using a loop) of sumInts
that takes a positive int value, n, and returns the sum of the
first n integers.
- Next, try writing a recursive version of the sum of n ints
function.