Page 2: Pascal’s Triangle and Efficiency
Unit 8, Lab 5, Page 2
Here’s one solution to the pascal block:

There are two base cases, the beginning and end of a row. The recursive case calculates the sum of two values in the previous row.
=
+ 
You are about to figure out how many recursive calls are needed to compute a value in Pascal’s Triangle.
What could you insert inside the block to track this?
Modify your
pascalblock to keep count of how many times the block is executed.
How many times is the
pascalblock called when calculating
?
What happens when you call
?
Many of the recursive calls are redundant. For example,
will call
many times, asking for the same information.
Track what
actually calls to see the redundancy.
Take It Further…
This technique is called memoization. This is not a typo; there’s no “r” in the word. Each recorded answer is like a “memo”.
Use a list structure to keep track of already-computed values of
pascal, so that when the same inputs are given again, the function looks up the saved value instead of making more recursive calls.
There is a direct formula for
pascalthat is often used:Build a version of
pascalthat uses the formula. Compare the efficiency of this version to the recursive version, and determine whether each version runs in constant time, linear time, quadratic time, or exponential time.