Unit 15 (Recursion) Assignment (Optional)
AP Computer Science 2021-22 / Dr. Kessner

Code exercises

Here are some optional fun code exercises to practice thinking about algorithms. Choose any that look interesting to you. Make sure to write unit tests to verify that your code is behaving the way you expect it to.

Recursion

  • Write a recursive function that takes a single integer n as input and returns the sum of integers from 1 to n.

  • Pick a geometric sequence (e.g. 2, 6, 18, 54, …) Write a recursive function that takes a single integer n as input and returns the nth term in the geometric sequence.

  • Write a recursive function that takes a single integer n as input, and returns the nth Fibonacci number.

  • Calculate the binomial coefficients (Pascal’s Triangle) recursively using the recursion formula $C(n,k) = C(n-1,k-1) + C(n-1,k)$.

  • Explore recursion in computer graphics (in Processing or in your project).