Processing+-+Recursion

Warm-up: Write a function that calculates the nth triangular number. For example, code format="java" println(triNum(7)); code outputs 28.

Then, try to wrap your head around this web app: Recursive Drawing.

Solving problems using **recursion** is the idea that you can use a problem to solve that problem, by using a smaller version of the problem. Recursion shows up in the real world in file systems, fractals, audio feedback and artwork. It's also used in mathematics and logical proofs (proof by induction). There are many programming languages based entirely on recursion, the most common of which is LISP.

Let's take a look at how recursion can be used to solve problems by examining our problem from above.

When you solve the triangular numbers problem, you likely used a loop (if you didn't, try to write one now!) We call this an **iterative** approach, because your loop //iterates// through several times before producing the result. You could also use the mathematical formula: math S = \frac{n(n+1)}{2} math to find the solution. (Write a solution using this formula if you haven't already. You should have two solutions now.)

A third way to look at this problem is as follows. The sum of all of the numbers from 1 to n is: The (sum of all of the numbers from 1 to n-1) + n

And the (sum of all of the numbers from 1 to n-1) is: The (sum of all of the numbers from 1 to n-2) + (n-1)

So the total sum is: The (sum of all of the numbers from 1 to n-2) + (n-1) + n

And we could keep going until we reach, the total sum is: 1 + (2) + (3) + ... + (n-1) + n

If we wrote this as pseudocode, we would get: code format="java" if n is 1: return 1 otherwise: return (sum of 1 to n-1) + n code Now, can you use this approach to write a recursive function in Processing? (Try it now!)

The trick to using recursion is that you must consider a **smaller** version of the problem each time you call the function again. There must also be a case that is the absolute smallest number or result you could get. We call this the **base case**. The case in which the function calls itself is called the **recursive case**.

There is more info in this chapter from the book //Think Like a Programmer//: (Read up to " Applying Recursion to Dynamic Data Structures." This book is in C++, but in the first part of the chapter, the examples will also work in Processing. We will look at Recursive Data Structures in Processing soon!) Try these problems when you have read the chapter - one of the solutions is in the chapter!

You try it: 1. Write a function factorial which calculates the factorial of a number. Remember: 1! = 1; 0! = 0; and math n! = n \times (n-1) \times (n-2) \times \cdots \times 1 math Here is an approach you could take:

code format="java" //Approach:

//base case: 1! = 1 and 0! = 1

//recursive case: n! = (n-1)! x n int fact(int n) { if n is 1 or n is 0: return 1 otherwise: return fact(n-1) * n

}

code

2. Write a recursive method to calculate the power of a number. Here is the method signature and approach: code format="java" //Approach:

//base case: n = 0 then a^n = 1

//recursive case: n > 0 then a^n = n * a^(n-1) int pow(int a, int n) {

}

code

3. Write a recursive method to calculate the nth Fibanocci number. The Fibanocci numbers are defined as: math F_{1} = 1 F_{2} = 1 F_{n} = F{n-1} + F_{n-2} math

Use your recursive method to calculate the 20th Fibanocci number.