Unit 15 Assignment (optional)#

Code exercises#

Here are some optional fun code exercises to practice thinking about recursion. 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)\).

  • Implement binary search with a recursive function.

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